Use ActionForm to get all the parameters

xiaoxiao2021-03-06  225

Brothers who have used Jakarta Struts know that Actionform's use method and her role she play in the Struts framework, but are you tired and exhausted in order to write a lot of setter and getter methods to ACTIONFORM? Indeed, I am tired, so I have to seek new methods. My first reflection is to use the MAP interface, put all the form parameters into the HashMap, then get this MAP in the action, and the operation of the business logic. Of course I have to go to experiment, practice is the only standard for testing truth. First, you must write a class that extends an actionform. The code is as follows (due to the space, I only list the key part 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 returniMap.get (key); 27} 28}

Next, explain the above code: Chain 11 defines an instance variable of a HashMap class. This MAP is used to store the parameter value in the form, as a key value pair, the setformvaluemap () method of line 13 The GetFormValueMap () method for setting an instance variable for obtaining instance variables FormValueMap, which will call in the Action class, replacing the previously called a large number in the Action class. () Method; Line 21 is a key method, and setFormValue () This method is to replace a large number of setxxxx () methods previously in ActionForm, which is used to accept input values ​​on the form. Note the parameters of this method, Key is a string type, used as the key, value is Object type, which means that each input value conveyed by the form. The 25th line is a getFormValue () method for returning a value object from the MAP according to Key. Ok, Actionform's mission is completed, but there is still a big distance from the goal, and only one-third of work is completed. We continue! With an actionform, you have to write an action class, the code is as follows (due to the space, I only list the key part 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.formean. CustomForm10 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 row of the CustomAction class is set out in line 21-22, using the getFormValuemap () method defined in the Customform class, collect all the submission parameters in the form, save the previous To use a lot of getxxx () methods to get the form of forms from ActionForm. Ok, let's write a JSP page, because our ActionForm accepts the parameter value using a special method, so a special signature is also used when writing a form page. code show as below:

< TD>;
;
< TD Height = "25"> User Name
; password
;

The above is a user login interface, pay attention to the red part, this signature is consistent with the setFormValue () signature in the Customform class, paying attention to case, Struts uses Java reflex mechanism to find the appropriate method call. FormValue (username) The string in parentheses can be mapping that represents the fields in the database in the database, or you can customize your own string, which will be saved as the Map of Key. Ok, finally have 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 segment:

Enter the following code in the segment:

Datually, all tasks are completed. In order to detect if you can run correctly, I add to the following test code in the Action class:

1 set set = formvalues.Entryset (); 2 item 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 will print out all forms submitted by all forms in the Tomcat console, you can also use the log4j component in the Struts to enter in the log file. Ok, let's write here, the above is to obtain the data submitted by Map at one time, and all the work contents in the action, I hope to use the brothers who use Struts to prompt and inspire.

转载请注明原文地址:https://www.9cbs.com/read-128869.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.045, SQL: 9