Struts create check boxes with dynamically selected elements

Source: Internet
Author: User
Tags array contains final interface key variables return string
Create | dynamic | check box in the user interface design, the checkbox group is not as popular as its own-multiline selection box. They basically do the same thing, which is to select a set of options mapped to a single name property. When used in a group, the check box performs the same functions as the MultiRow selection box, but they occupy more screen space. This can be beneficial when you want the user to be able to see all the options before selecting one or more options.

Although multiline selection boxes generally provide a better impression when the options are few, the checkbox group is a better choice for enterprise applications when the selection box must be dynamically rendered and contains preselection functionality. Fortunately, using the Struts framework makes it easy to create dynamic check box groups.

In this article, I'll introduce a simple trick: to use the
Let me start with a simple string[] array that uses the checkbox element, which contains the peak height of the Himalayas. Then I will create another string[] array, containing the Selectedmountains, representing the check box that is already selected. The preselection of a check box is generated at the intersection of two arrays. If the initial array of Selectedmountains is empty, then all check boxes will initially appear unchecked.

   Create a dynamic check box

The trick to creating a dynamic check box consists of three main sections:
    • A form bean that holds the string[] array for the check box and an string[array that represents the selected check box.

    • A JSP with a form that displays a check box when needed.

    • A simple Action class that turns from a form page to a display page.
Please note that the "Himalayas" sample is very simple. The fields used to populate the check boxes should come from more complex models, such as the model, which identifies the user, selects the fields to display, and then pre-selected the options that the business object deems necessary. I used a simple model to better demonstrate the user interface features of Struts. The code example uses the JSP scripting language to express clarity.

1th step. Create a form bean

Let me start by creating a Struts form bean that contains the information that is required to fill the check box. Note that the Testform.java in Listing 1 contains the getter and setter for two example string[] array variables. Array Mountains all options for the example check box, and the array selectedmountains represents the preselected element that is displayed as selected in the browser.

In addition to the checkbox representing the initially selected check box, Selectedmountains also represents the check box selected by the user when the form is processed. (It represents only the final selected element.) When the page is requested, a check box is displayed. When I iterate between them, the check box element that matches the selectedmountains is the selected element.

Listing 1 shows the complete code for 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 Checkboxtestform extends Actionform {//Instance Variables/*mountains "pre-selected" ... * * Private STR Ing[] Selectedmountains = {"Everest", "K2", "Lhotse"}; /*the Ten tallest mountains to iterate through*/private string[] Mountains = {"Everest", "K2", "Kangchenjunga", "Lhotse", "M Akalu "," Kangchenjunga South "," Lhotse Middle "," Kangchenjunga West "," Lhotse Shar "," Cho Oyu "}; /*getter for selectedmountains*/public string[] Getselectedmountains () {return this.selectedmountains;}/*Setter for SE lectedmountains*/public void Setselectedmountains (string[] selectedmountains) {this.selectedmountains = Selectedmountains; }/*getter for the "mountains*/public string[" Getmountains () {return this.mountains;}/*setter for themountains*/public void Setmountains (string[] mountains) {this.mountains = Mountains;}}  

2nd step. Writing JSP Code

Next, I'm going to write the JSP code for the page and pass the Testform.java information to the view layer. When writing this code, the key is to import the corresponding Struts tag library into the JSP. The JSP code in Listing 2 represents a simple form that displays a check box with the corresponding box selected:

Listing 2. JSP with a form

 <% @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 ... --> 
  
    > 
   

<%--Create the CheckBox and Selected attribute--> <%--Create the label, note that "BR" tag would format it vertically-->

<%--Some more HTML code, etc ... -->


Notice that I use Struts to mark the text, use to represent the HTML check box, iterate through the array with the tag, and create the appropriate content. My form is instantiated in the JSP by the tag.

The next step is to iterate over the mountains field in the tag. In doing so, I created a variable (mountain) that fills the check box and uses the tag to give it a label. To create the selected property in the check box, I want to use the and tags again. The property properties in the tag are populated by the Selectedmountains field. When Selectedmountains equals Mountain, Selectbox is selected.

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.