MAP is used as the actionform attribute to dynamically add the "attribute" of actionform"

Source: Internet
Author: User
Tags map class

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 {
This. style. Display = 'none'; codehighlighter1_1__1__open_text.style.display = 'none'; codehighlighter1_1__closed_image.style.display = 'inline'; interval = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_1__open_image.style.display = 'inline'; codehighlighter1_1__1__open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif "alt =" "align =" TOP "> public class testform extends actionform {
7 private hashmap <string, Object> map = new hashmap <string, Object> ();
8
9 {
This. style. Display = 'none'; codehighlighter1_236_265_open_text.style.display = 'none'; codehighlighter1_236_265_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; encoding = 'none'; codehighlighter1_236_265_open_image.style.display = 'inline'; codehighlighter1_236_265_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif "alt =" "align =" TOP "> Public object getvalue (string key ){
10 return map. Get (key );
11}
12
13 {
This. style. Display = 'none'; codehighlighter1_1__344_open_text.style.display = 'none'; codehighlighter1_1_1__344_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_1__344_open_image.style.display = 'inline'; codehighlighter1_1__344_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif "alt =" "align =" TOP "> 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 {
This. style. Display = 'none'; codehighlighter1_290_484_open_text.style.display = 'none'; codehighlighter1_290_484_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; outputs = 'none'; codehighlighter1_290_484_open_image.style.display = 'inline'; codehighlighter1_290_484_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif "alt =" "align =" TOP "> public class testaction extends action {
11 {
This. style. Display = 'none'; codehighlighter=435_482_open_text.style.display = 'none'; codehighlighter=435_482_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter=435_482_open_image.style.display = 'inline'; codehighlighter=435_482_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif "alt =" "align =" TOP "> 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 {
This. style. Display = 'none'; codehighlighter1_2_48_open_text.style.display = 'none'; codehighlighter1_2_48_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_2_48_open_image.style.display = 'inline'; codehighlighter1_2_48_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif "alt =" "align =" TOP "> <% @ taglib uri ="/tags/Struts-html "prefix =" html "%>
2
3 <HTML: Form Action = "/test. Do" method = "Post">
4 <HTML: Text property = "value (a)"/>
5 <HTML: Text property = "value (B)"/>
6 <HTML: Submit> submit 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 {
This. style. Display = 'none'; codehighlighter1_2_48_open_text.style.display = 'none'; codehighlighter1_2_48_closed_image.style.display = 'inline'; inline = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_2_48_open_image.style.display = 'inline'; codehighlighter1_2_48_open_text.style.display = 'inline ';
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif "alt =" "align =" TOP "> <% @ 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>

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.