Use actionform to retrieve all parameters of the form at a time

Source: Internet
Author: User
Brothers who have used Jakarta Struts know how to use actionform and her role in the Struts framework, but are you tired and tired of writing a lot of setter and getter methods for actionform? Indeed, I am tired of it, so I want to seek new methods. In my first reflection, I want to use the map interface to put all form parameters into hashmap, get the map in the action, and perform operations in the business logic. If you have an idea, you must experiment. practice is the only criterion for testing truth.
First, write a class that extends actionform. The Code is as follows (due to the length, I only list the key parts of the Code ):

12 package com. jacktan. Struts. formbean;
34 import java. util .*;
5 import javax. servlet. http .*;
6 Import org. Apache. Struts. Action .*;
8 public class customform extends actionform {
10 private map formvaluemap = new hashmap ();
12 public void setformvaluemap (MAP p_formvaluemap ){
14 This. formvaluemap = p_formvaluemap;
15}
16 public map getformvaluemap (){
18 return this. formvaluemap;
19}
20 public void setformvalue (string key, object Value ){
22 formvaluemap. Put (Key, value );
23}
24 public object getformvalue (string key ){
26 return formvaluemap. Get (key );
27}
28}

The above code is explained one by one below:

Row 11th defines an instance variable of the hashmap class. This map is used to store the parameter values submitted in the form and exist as key-value pairs;
The setformvaluemap () method of row 13th is used to set the instance variable formvaluemap;

The getformvaluemap () method of row 17th is used to obtain the instance variable formvaluemap. This method will be called in the action class, instead of calling a large number of (customform) forms in the action class. getxxx () method;

Row 21st is the key method. The setformvalue () method replaces a large number of setxxx () methods in the actionform before and is used to accept the input values passed in the form. Note that the key of this method is of the string type and is used as the key of map. The value of this method is of the object type. This parameter indicates the input values transmitted from the form.

Row 25th is the getformvalue () method, which is used to return a value object from the map based on the key.

Now, the actionform task has been completed, but there is still a long way to go from the target. At present, only 1/3 of the work has been completed. Let's continue!

With actionform, the next step is to write the action class. The Code is as follows (due to the length, I only list the key parts of the Code ):

12 package com. jacktan. Struts. Action;
34 import java. util .*;
5 import javax. servlet. http .*;
6 Import org. Apache. Struts. Action .*;
8 Import com. jacktan. Struts. formbean. customform
10 public class customaction extends action {
12 public actionforward execute (actionmapping p_mapping,
15 actionform p_form,
16 httpservletrequest p_request,
17 httpservletresponse p_response)
18 throws exception {
21 map formvalues = (customform) p_form). getformvaluemap ();
23 return p_mapping.findforward ("success ");
25}
26}

The key rows of the customaction class are in line 21-22. Use the getformvaluemap () method defined in the customform class to collect all the submitted parameters in the form at a time, saves the previous practice of using the getxxx () method to retrieve form values from actionform.
Now, we need to compile the JSP page. Because our actionform uses special methods to accept parameter values, we also need to use special signatures when writing form pages. The Code is as follows:

<HTML: Form Action = "login">
<Table width = "100%" border = "0">
<Tr>
<TD width = "3%" >;</TD>
<TD width = "31%" >;</TD>
<TD width = "66%" rowspan = "6" valign = "TOP">
<HTML: errors/>
</TD>
</Tr>
<Tr>
<TD >;</TD>
<TD Height = "25"> User Name </TD>
</Tr>
<Tr>
<TD >;</TD>
<TD Height = "25">
<HTML: Text property = "formvalue (username)" size = "12" maxlength = "12"/> </TD>
</Tr>
<Tr>
<TD >;</TD>
<TD Height = "25"> password </TD>
</Tr>
<Tr>
<TD >;</TD>
<TD Height = "25">
<HTML: Password property = "formvalue (password)" size = "12" maxlength = "12" redisplay = "false"/>
</TD>
</Tr>
<Tr>
<TD >;</TD>
<TD Height = "25">
<HTML: Submit value = "login"/> <HTML: button property = "close" value = "close" onclick = "close ()"/>
</TD>
</Tr>
</Table>

The above is a user login interface. Pay attention to the red part. This signature must be consistent with the setformvalue () signature in the customform class. Pay attention to the case, struts will use the Java reflection mechanism to find the appropriate method call. The string in formvalue (username) parentheses can be a ing of the input field in the database or a custom string, which is saved as the map key.

Okay, finally to complete, the last thing to do is to set the struts configuration file, open the struts-config.xml file,

Enter the following code in the <form-beans> section:

<Form-beans>
<Form-bean
Name = "customform"
Type = "com. jacktan. Struts. formbean. customform"/>
</Form-beans>

Enter the following code in the <action-mappings> section:

<Action
Path = "/login"
Type = "com. jacktan. Struts. Action. customaction"
Name = "adddeviceform"
Scope = "request">
<Forward name = "login" Path = "Main. jsp"/>
</Action>

All the tasks have been completed. To check whether the operation can be correctly run, I add the following test code to the action class:

1 Set set = formvalues. entryset ();
2 iterator it = set. iterator ();
3 while (it. hasnext ()){
4 map. Entry me = (Map. Entry) it. Next ();
5 system. Out. println (string) me. getkey );
6 system. Out. println (string) me. getvalue );
7}

The above code prints the parameter values submitted in all forms on the Tomcat console. You can also use the log4j component in struts to input them in the log file. Now, let's write it here. The above is all the work content that is called in the action by using map to obtain the data submitted in the form at one time, I hope to give some tips and inspiration to everyone who uses STRUTS for development.

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.