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: