In this article, I will introduce the Nested Struts tag
See the download area to get a complete sample source code; if you need to download the Struts framework, see Resources.
Five steps
For the sake of simplicity, I use the same working example as the last in the dynamic check box to demonstrate the radio button. My simple user interface uses a radio button element to display the String [] array of Himalayas, the second SelectAin String array represents the selected select button. After creating a button, call a JavaScript function to preselect the radio button.
According to the example of Himalaya, the 诀窍 诀 窍 单 单 单 单 单 部分 部分 部分 部分 部分 部分 部分 部分:
A fake data class that accommodates Mountains and SelectAin data. A form bean, a string [] array and a specially selected button for the radio button. A JSP with a form showing the radio button in the desired state. A JSP, the elements selected in the form. A Action class, go from the form page to the display page.
The main difference between the radio button and the dynamic check box of the last school is: Struts does not provide a tool that automatically creates a Selected value, and this is usually required to create a dynamic radio button. Although the Checkbox and SELECT input types are quite simple in pre-selecting the function in the Struts JSP tag, the Radio Button input type requires different solutions. Fortunately, this requirement can be implemented using information from form beans and a few line JavaScript code, such as step 3.
Step 1. Create a data layer
I do a fake data class, and the data from the business layer is rendered to the application view layer, and the view layer is what I want to consider. The class named FakeData contains two static methods, such as Listing 1:
Listing 1. Fakedata.java
/ ** * Class Fakedata - Repesents the business logic * / public class fakedata {/ ** * Data for mountains * / public static final string [] mountains = {"everest", "kangchenjunga", "lhaotse" "," Makalu "," Cho Oyu "}; / ** * Data for Slected Mountain * / public static final string SELECTED_MOUNTAIN =" kangchenjunga ";
Creating a false data layer is a useful user interface development practice, because the continuous storage layer to be used for the final application is usually not seen for front-end developers. Therefore, it is not necessary to wait for the background team to complete the work, which can easily develop a fake data layer to simulate the API and functionality to be sent. Using a fake data layer, you can develop and reduce dependence on other teams. With a fake data layer, you can also define an API connection to other parts of the project and make sure there is less problem in integrating all parts.
Step 2. Create a form bean
The value that ultimately populates the application may come to a framework that is more complicated than the framework shown in Listing 1. For more beautiful examples, there is a good news that the form bean in Listing 2 does not have to do any heavy work, so it is just a simple JavaTM object with getter and setter methods. The actual value is inserted when the constructor is called. Listing 2. Radiotestform.java
package com.strutsrecipes; import org.apache.struts.action.ActionForm; / ** * Radio Button Test Form to show an array of radio buttons and * / public class RadioTestForm extends ActionForm {// -------- ---------------------- Fields --------------------------- --- / ** * The success selected mountain * / private string selectedmountain; / ** * The list of mountains for the radio button * / private string [] mountains; // ------------ --------------- Constructors ------------------------- / ** * Constructor - Using fakedata ... * / public radiotestform () {this.selectedMountain = fakedata.selected_mountain; this.mountains = fakedata.mountains;} // ------------------- - getter / setter methods --------------------- / ** * getter for the mountains * * @return the mountains array * / public string [] getMountains ( ) {return this.mountains;} / ** * setter for the mountains * * @Param m the mountains array * / public void setMountains (String [] m) {this.mountains = m;} / ** * Getter for selectedMountain * * @return the selected mountain * / public String getSelectedMountain () {return this.selectedMountain;} / ** * Setter For selectedMountain * * @Param sm the selectedmountain * / public void setselectedmountain (string sm) {this.selectedMountain = SM;}}
For the sake of clarity, I contain all Java code for the form bean. Note that Kangchenjunga is listed in the SelectAin and Mountains fields, and is instantiated in the constructor and fill it with the FakeData class. Now, I have enough information to pass Kangchenjunga to JSP, as the initial value of Preselected. Step 3. Create a radio button JSP
Listing 3 contains the JSP code of the form page, which contains the radio button and the pre-selected value in the form page. Note the relationship between Java files and logic, HTML, bean tags and JavaScript functions at the bottom of the form. I iterate on the Mountains collection to create a radio button. After this work is completed, I add JavaScript and fill the value of SelectedMountain, and compare the radio button array to select the correct button.
Listing 3. JSP containing radio buttons and preselected values
<% @ Taglib URI = "/ Tags / Struts-Bean" prefix = "bean"%> <% @ Taglib URI = "/ tags / struts-html" prefix = "html"%> <% @ Taglib URI = "/ Tags / struts-logic "prefix =" logic "%>
<