Starting from Struts 2.1.3, Filterdispatcher becomes strutsprepareandexecutefilter
Learning is how the program actually calls the action class based on the XML file, and how the value of the form is propagated to the action class. Reading how also can not understand, at this time there is a teacher is very important, reading to their own thinking, sometimes did not learn some technology, will be tangled how to achieve AH. Well, there's a suspicion of advertising.
OK, after watching Struts2 's video, know that all this is a reflection of the result, previously just know the reflection, and never to understand it. Actually, it's very important.
<span style= "FONT-SIZE:18PX;" >import java.util.*;import java.lang.reflect.*;p ublic class mockfilter{public static void Main (string[] args) throws exception{//Analog Request Parameters/*<action Name= "useraction" class= "useraction" method= "regist" ><result name= "Success" >VIEW.JSP</RESULT></ACTION>*///assumes that filter received useraction.action request//1, filter GET request parameter// Request.getparametermap ()//simulation gets all parameters in Request map<string,string> Paramap = new hashmap<string,string> (); Paramap.put ("name", "Zhangsan");p aramap.put ("Pass", "1234");//2, Search Struts.xml, find action according to name, create an action instance from the class attribute, call the function specified by the method property string Clazzprop = "useraction";// Reflection: Gets the class object corresponding to the action class class Actionclazz = Class.forName (Clazzprop);//Create an action instance with reflection object Actioninst = Actionclazz.newinstance ();//The statement indicates that the action class must have an argument-free constructor for (String Para:paraMap.keySet ()) {///Get the Setparameter () function, Gets the parameter name corresponding to the Set function method setter = Actionclazz.getmethod ("Set" + para.substring (0,1). toUpperCase () + para.substring (1), String.class);//Gets the parameter value corresponding to the request parameter, string value = parAmap.get (para);//Use the action instance as the caller, call setter method Setter.invoke (Actioninst,value);//class instance and parameter value}//3, Call the method property specified by reflection again according to methods exec = Actionclazz.getmethod ("regist");//Description action is handled by the parameterless function String result = (string) Exec.invoke (Actioninst), if (Result.equals ("Success")) {System.out.println ("Analog View page!!! ");}}} Class Useraction{private string name;private string pass;public void SetName (string name) {This.name=name;} Public String GetName () {return name;} public void SetPass (String pass) {this.pass=pass;} Public String Getpass () {return pass;} Public String regist () {System.out.println ("name----:" +name); SYSTEM.OUT.PRINTLN ("Pass----:" +pass); return "Success";}} </span>
The above procedure is a simple reflection-based controller principle display.
1. The browser sends the request: Abc.action. Action suffix can be preserved or omitted
2. Requests sent by the browser are Strutsprepareandexecutefilter intercepted.
--------because we specify Strutsprepareandexecutefilter in Web. XML to filter all requests
3. Strutsprepareandexecutefilter will create an action instance.
If we ask Abc.action,strutsprepareandexecutefilter to search for an action configuration named ABC in the Struts.xml file, use reflection based on the class attribute To create an action instance
4. Call
map<string,string> Paramap = Request.getparametermap ();//Return request parameter
Use Loop (String ParaName:paramMap.keySet ()) {
Action class. GetMethod (the first letter of the "set" +paraname capital);
Get the value of the request parameter
String paravalue = Paramap. Get (Paraname);
Invokes the setter method with the action instance as the caller, passing the request parameter value as a parameter value
Setter.invoke (Actioninst,paravalue);
}
5, again "by reflection" call method property specified by the methods
6. Jump to the specified page according to the Name property of the Struts.xml result element and the physical view resource
With this process, you can understand the process.
From: Li Gang struts2 video
The process of struts2