Introduction: Struts Recipes co-author Danilo Gurovich from where George Franciscus stopped, describes the easy to use struts trick to create a dynamic selection check box.
In the user interface design, the checkbox group is not as popular as its same-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.
See download for the complete sample source code. You should have everything you need to follow this article. If you need to download the Struts framework, see Resources.
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.