In general, we need to define the corresponding private attributes for the corresponding form in ActionForm, and then set and obtain the form data through Getter and Setter.
However, if there are many forms of data fields, you need to define a private attribute for each form field in ActionForm and define the corresponding Getter and Setter methods.
There are two solutions: one is to define a Map private attribute in ActionForm to dynamically generate the data field submitted by the form; the other is to use DynaActionForm.
Here, I would like to introduce the first method in an example.
First create the ActionForm class: TestForm
1 package form;
2
3 import org. apache. struts. action. ActionForm;
4 import java. util. HashMap;
5
6 public class TestForm extends ActionForm {
7 private HashMap <String, Object> map = new HashMap <String, Object> ();
8
9 public Object getValue (String key ){
10 return map. get (key );
11}
12
13 public void setValue (String key, Object value ){
14 map. put (key, value );
15}
16
17}
Note that the second row defines a private variable, which is a Map class.
Rows 9th and 13 define the Getter and Setter methods. Here, the Getter method requires a parameter to pass the name of the form field (that is, the dynamic attribute). The Setter method requires two parameters, the form domain names and corresponding values are different from the Getter and Setter of the traditional ActionForm.
Then create the Action class: TestAction
1 package action;
2
3 import org. apache. struts. action. Action;
4 import org. apache. struts. action. ActionMapping;
5 import org. apache. struts. action. ActionForm;
6 import org. apache. struts. action. ActionForward;
7 import java. util. HashMap;
8 import javax. servlet. http .*;
9
10 public class TestAction extends Action {
11 public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
12 return mapping. findForward ("success ");
13}
14}
Row 3: As this is an example, "*" is used to replace the specific class for convenience. During programming, we still need to cultivate good habits to introduce the corresponding classes to avoid conflicts.
This class is very simple, just pass the request to a corresponding page named "success" forward in Struts, see the configuration in the struts-config.xml listed below.
Then the JSP page of the input form: input. jsp
1 <% @ taglib uri = "/tags/struts-html" prefix = "html" %>
2
3 4 5 6 7 Line 3 introduces Struts's html Tag Library
Note the rows 4th and 5. If the traditional ActionForm is used, the property value should correspond to the name of a private attribute in the ActionForm, but here, recall that the Getter and Setter methods in the above ActionForm have one parameter and two parameters respectively. Here, the property will automatically call the Setter method through the reflection mechanism, "value () "value" is the name of the Map in ActionForm, and "a" in brackets is the name of the dynamically generated form field, it corresponds to the first parameter "key" in the Getter method, and the value entered by the user in the form corresponds to the second parameter "value" in the Setter method ".
Here, two form fields named "a" and "B" are created dynamically and stored in Map.
Then, let's take a look at the page forwarded by success. jsp, that is, Action.
1 <% @ taglib uri = "/tags/struts-bean" prefix = "bean" %>
2
3 <bean: write name = "test" property = "value (a)"/>
4 <bean: write name = "test" property = "value (B)"/>
Line 3 introduces the Struts bean tag library.
Note that row 2nd references the ActionForm named "test" (for detailed settings, see the struts-config.xml ). This label is used to display the specified attribute value of the output ActionForm. Note that "value" is the name of Map in ActionForm. It automatically calls the Getter method of ActionForm through the reflection mechanism. Recall that the Getter method in the above ActionForm has a parameter "key ", here, "a" and "B" are actually two form field attribute names dynamically generated on the input page. Here, the "key" in the Map Class Attribute "value" of ActionForm named "test" is displayed as the corresponding "value" of "a" and "B" respectively ".
Through this design, we have completed the function of dynamically generating form field attributes. The advantage of this design is that you can design your form field attributes without modifying ActionForm, the ActionForm encoding is also greatly reduced.
Finally, let's take a look at the corresponding configuration in the struts-config.xml (listing only the relevant configuration snippets ):
1 <struts-config>
2
3 <form-beans>
4 <form-bean name = "test" type = "form. TestForm"/>
5 </form-beans>
6
7 <action-mappings>
8 <action name = "test" path = "/Test" type = "action. TestAction">
9 <forward name = "success" path = "/pages/success. jsp"/>
10 </action>
11 </action-mappings>
12
13 </struts-config>
Reference: Struts In Action, chapter 5, section 4