In the user interface design, the checkbox group is not as popular as its similar class - multi-line selection boxes. They basically do the same thing, that is, select a set of options mapped to a single Name property. When used in groups, the functions performed by the check box are actually the same as the multi-line selection box, but they occupy more screen space. This will be good when you want users to see all options before choosing one or more options.
Although the multi-line selection boxes typically provide better viewing when the options are not available, the selection box must be dynamically rendered and include a pre-selection function, and the enterprise application check box group is a better choice. Fortunately, using the Struts framework can easily create a dynamic checkbox group.
In this article, I will introduce a simple trick: use struts
I first display the simple String [] array from the check box element, and the array contains the peak height of the Himalayas. Then, I will create another String [] array, contain SelectAins, represents the check box already selected. The preselection of the checkbox will be generated in the intersection of the two arrays. If the initial array of SelectMountAins is empty, then all checkbox will initially appear in unchecked.
See Download Get Complete Example Source Codes. You should have everything you need to follow this article. See Resources if you need to download the Struts framework.
Create a dynamic checkbox
Creating a dynamic check box contains three main parts:
A form bean, a string [] array and representation of the check box for the check box, and the String [] array. A JSP, with a form, display the check box when needed. A simple Action class, go from the form page to the display page.
Note "Himalayas" example is very simple. Fields used to populate the check box should come from a more complex model, such as such a model, which can identify the user and select the field to display, and then the business object is read in advance. I use a simple model to better demonstrate the user interface function of Struts. The code example uses the JSP script language to express it clearly.
Step 1. Create a form bean
I started from creating the Struts form bean, which contains the information you need to fill the check box. Note that TestForm.java in Listing 1 contains two example string [] array variables Getter and Setter. Array MountAins represents all options for the Examples The selection box, the array SelectEDMountAins represents the preselected elements in the browser.
In addition to the check boxes representing the initial selection, SelectEdMountAins also selects the check boxes by the user when the form is handled. (It only represents the final elements.) When the request page, the check box will appear. When I iterates between them, the check box element that matches SelectAins is the selected element.
Listing 1 shows the full code of Testform.java:
Listing 1. Testform.java
package com.strutsrecipes; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org. apache.struts.action.ActionMapping; public final class CheckboxTestFormextends ActionForm {// Instance Variables / * Mountains "pre-selected" ... * / private String [] selectedMountains = { "Everest", "K2", "Lhotse"} ; / * the Ten Talest Mountains to Itereate Through * / private string [] mountains = {"Everest", "Kangchenjunga", "Kangchenjunga", "Lhotse", "Makalu", "Kangchenjunga South", "Lhotse Middle", " Kangchenjunga West "," Lhotse Shar "," Cho Oyu "}; / * Getter for selectedMountains * / public String [] getSelectedMountains () {return this.selectedMountains;} / * Setter for selectedMountains * / public void setSelectedMountains (String [] selectedMountains) {this.selectedMountains = selectedMountains;} / * Getter for the mountains * / public String [] getMountains () {return this.mountains;} / * Setter for the mountains * / public void setMountains String [] mountains) {this.mountains = mountains;}} Step 2. Write JSP code
Next, I want to write the JSP code of the page, pass the information of TestForm.java to the view layer. When writing this code, the key is to import the corresponding Struts tag library into JSP. Listing 2 The JSP code represents a simple form that displays the corresponding box in the check box:
Listing 2. JSP with forms
<% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%> <% @ Taglib URI = "http://jakarta.apache.org/struts/tags -bean "prefix =" bean "%> <% @ Taglib URI =" http://jakarta.apache.org/struts/tags-logic "prefix =" logic "%> <% - HTML Code, ETC .. ->
The next step is to iterate on the Mountains field in
Step 3. Write an Action class
The final step is to write an Action class. Listing 3 compared to other lists, there are not many things. I am doing just get the STRING [] array of SelectAins, and make it available for pages: Listing 3. Action of the form
import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import javax.servlet.ServletException ; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;. / *** A simple Action for Checkbox test ** @author Danilo Gurovich * / public final class CheckboxtethctionExtends action {// ------------------------------------------- --------- / *** The execute method ** @param mapping ActionMapping * @param form CheckboxTestForm * @param request HttpServletRequest * @param response HttpServletRespons * @return success to the confirmation page * @throws ServletException not thrown, but could be! * @throws Exception ditto. * / public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, Exception {// Extract attributes neededStrin g [] selectedMountains = ((CheckboxTestForm) form) .getSelectedMountains (); System.out.println ( "htmlString RETURNED * / n" selectedMountains.toString ()); // Save the htmlString in the session for later ... Httpsession session = request.getsession (); session.setttribute (checkboxconstants.mountains; return (mapping.findforward ("success));}}
Expansion hiMalayas
With this code, the job is completed, almost show the results! Users can now submit the JSP form and view the results in the corresponding page referenced by the Action class. The code segment in Listing 4 shows the list of checkboxes selected in the form of the simple JSP page:
Listing 4. The result of the check box selection
<% @ Taglib Uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%> <% @ Taglib URI = "http://jakarta.apache.org/struts/tags -bean "prefix =" bean "%> <% @ Taglib URI =" http://jakarta.apache.org/struts/tags-logic "prefix =" logic "%> <% - HTML Code, ETC .. ->
<% - Some More HTML Code, ETC ... -> This kind of work method
The key to this 诀窍 is that the fields in the form bean are passed to the page. View related JSP code helps clarify this. Once the form bean is instantiated:
The next step creates a check box for each Mountains in the Mountains variable of the Java class. To do this, I must iterate like this in the string [] array:
Using the
Here you can see the effect of the
Note that the Property property is populated by SelectAins, which is the variable I have selected. When this variable corresponds to the
By using the Struts LabelValueBean class instead of a simple String [] array, you can extends this to the dynamic check box to create a different label for the check box. First add LabelValueBeans to java.util.list. Then iterate the list, release the LabelValueBeans tag and value to the appropriate location. This slightly complex 诀窍 is the same as the dynamic check box, but its results are more suitable for the actual user interface design. Listing 5 shows the extended dynamic check box:
Listing 5. Add label to dynamic checkbox
logic: iperate>
Note that the big change here is to create LabelValueBean when it is iterated. Then use
Conclude
Struts provides excellent support for the dynamic presentation and preselection of checkboxes. This trick is the reason why Struts Recipes - I have discovered many theories and server information related to the Struts framework, but most of the user interface programming is ignored, or hidden. After I found a circle up and down, I gave up and wrote one of them myself. By combining different portions, I can create a dynamic check box system that suits my.
You will notice that the code example is set to fit the test hotbed that is used as a small control and layout idea for different user interface. In fact, I use it in most user interface examples in the book, just adjust the Action class and my model to suit the needs of the trick. I also use it in different ideas, so I don't have to spend too much time in the application I am processing for some things.
download
Description Name Size Download Method Sample Codej-SR3-Source.zip3249 KB FTP Posted in @
December 23, 2005 1:40 PM |
Comments (0)