The action is used to process the request operation, which is distributed by Strutsprepareandexceutefilter.
1. How action is created
1) Pojo Class (Plainoldjavaobjects simple Java object), does not need to inherit any parent class, implements any interface
1 Public class testaction {2 Public String Execute () {3 return "Success"; 4 }5 }
This approach is achieved through reflection by the STRUTS2 framework, in the following steps:
- The STRUTS2 framework obtains the full action class name by reading the struts.xml configuration file;
- Object = Class.forName ("Full class name"). newinstance ();
- method = Class.forName ("Full class name"). GetMethod ("execute");
- Method.invoke (object);
2) Implementing the Action interface
1 Public classTestactionImplementsAction {2 /**3 * Can not rewrite the Execute method, with a custom method, but4 * required to display the specified method name method= the method to be executed in the configuration file5 */6 @Override7 PublicString Execute () {8 return"Success";9 }Ten}
Action interface, define the default five logical view names:
Public Static FinalString SUCCESS = "SUCCESS";//Data Processing Success (Success page) Public Static FinalString none = "None";//page does not jump return null; Public Static FinalString error = "Error";//Data Processing send error (Error page) Public Static FinalString input = "INPUT";//incorrect user input data, typically used for form data validation (Input page) Public Static FinalString login = "Login";//Primary Authority Authentication (landing page)
Five logical views are used to resolve the action processing data, jump page
3) Inherit Actionsupport class (recommended)
In fact, the Actionsupport class itself has implemented the action interface, and can be used in the action of form validation, error message settings, read the internationalization of information three functions, so recommended.
1 Public classTestactionextendsActionsupport {2 /**3 * Can not rewrite the Execute method, with a custom method, but4 * required to display the specified method name method= the method to be executed in the configuration file5 */6 @Override7 PublicString Execute () {8 return"Success";9 }Ten}
2. Action Access
When you configure the <action> element, you do not specify the method property, which executes the Execute method in the action class by default.
1) Basic Access
The following action access path is available in the JSP page:
1 < href= "${pagecontext.request.contextpath}/book/update.action"> Test </a>
Configuration file for Struts.xml:
1 <Struts>2 <constantname= "Struts.devmode"value= "true" />3 < Packagename= "Default"namespace= "/book"extends= "Struts-default">4 <Actionname= "Add"class= "Cn.sunny.action.BookAction"Method= "Add">5 <resultname= "Success">/success.jsp</result>6 </Action>7 <Actionname= "Update"class= "Cn.sunny.action.BookAction"Method= "Update">8 <resultname= "Success">/success.jsp</result>9 </Action>Ten <Actionname= "Delete"class= "Cn.sunny.action.BookAction"Method= "Delete"> One <resultname= "Success">/success.jsp</result> A </Action> - <Actionname= "Search"class= "Cn.sunny.action.BookAction"Method= "Search"> - <resultname= "Success">/success.jsp</result> the </Action> - </ Package> - </Struts>
by combining the namespace of <package> with the Name property of <action> to compare with the request resource path, you can know which action to access, knowing which action class to access through the <action> class knows which method to access by means of method.
2) use a wildcard character
Use wildcards * To simplify struts.xml configuration
JSP page:
1 <ahref= "${pagecontext.request.contextpath}/book/book_add">Book_add</a><BR>2 <ahref= "${pagecontext.request.contextpath}/book/book_update">Book_update</a><BR>3 <ahref= "${pagecontext.request.contextpath}/book/book_delete">Book_delete</a><BR>4 <ahref= "${pagecontext.request.contextpath}/book/book_search">Book_search</a><BR>
The configuration of the action:
1 <Struts>2 <constantname= "Struts.devmode"value= "true" />3 < Packagename= "Default"namespace= "/book"extends= "Struts-default">4 <Actionname="*_*"class= "Cn.itcast.action." {1} Action "Method= "{2}">5 <resultname= "Success">/success.jsp</result>6 </Action>7 </ Package>8 </Struts>
Each "*" in the value of the Name property in <action> represents any string that is not 0 in length, and name= "*_*" indicates that the page access action name must be for a similar book_add.action, book _ Update.action form.
If a wildcard character is defined in the Name property, the wildcard character can be used in the Class property, the method property, the Name property of the <result> and the name of the returned JSP page, and {1} represents the second * for the first *,{2}.
3) Dynamic Method invocation
When you do not configure the method property in <action> and you do not want to execute the default Execute method, you can use the dynamic method call, which is accessed as "action name" + "!" + "Method Name":
<href= "${pagecontext.request.contextpath}/book/book!add"> Bookadd</a>
Action configuration:
1 <Struts>2 <constantname= "Struts.devmode"value= "true" />3 < Packagename= "Default"namespace= "/book"extends= "Struts-default">4 <Actionname= "book"class= "Cn.sunny.action.BookAction">5 <resultname= "Success">/success.jsp</result>6 </Action>7 </ Package>8 </Struts>
Note: There is one item in the Struts2 constant configuration that can be set to close a dynamic method call, which is allowed by default:
1 struts.enable.DynamicMethodInvocation = True
STRUTS2 Study Notes (iii)--action detailed