Struts2 Study Notes (3) -- Action details, struts2 Study Notes
Action is used to process request operations. It is distributed by StrutsPrepareAndExceuteFilter.
1. How to create an Action
1) POJO class (PlainOldJavaObjects simple Java object), no need to inherit any parent class, implement any interface
1 public class TestAction {2 public String execute() {3 return "success";4 }5 }
This method is implemented by the Struts2 framework through reflection. steps:
- The struts2 framework reads the struts. xml configuration file to obtain the complete Action class name;
- Object = Class. forName ("full Class name"). newInstance ();
- Method method = Class. forName ("full Class name"). getMethod ("execute ");
- Method. invoke (object );
2) Implement the Action Interface
1 public class TestAction implements Action {2/*** 3 * you can use a custom method instead of overwriting the execute method, however, 4 * in the configuration file, you need to display the specified method name method = method to be executed 5 */6 @ Override 7 public String execute () {8 return "success "; 9} 10}
In the Action interface, five default logical view names are defined:
Public static final String SUCCESS = "success"; // successful data processing (successful page) public static final String NONE = "none"; // return null is not redirected to the page; same effect: public static final String ERROR = "error"; // data processing sending ERROR (error page) public static final String INPUT = "input"; // user INPUT data ERROR, usually used for form data verification (input page) public static final String LOGIN = "login"; // main permission authentication (LOGIN page)
Five logic views are used to handle the data processing by Action and jump to the page
3) inherit the ActionSupport class (recommended)
In fact, the ActionSupport class has already implemented the Action interface and can use Form Verification, error information setting, and read internationalization information in the Action. Therefore, it is recommended to use it.
1 public class TestAction extends ActionSupport {2/*** 3 * you can use a custom method instead of overwriting the execute method, however, 4 * in the configuration file, you need to display the specified method name method = method to be executed 5 */6 @ Override 7 public String execute () {8 return "success "; 9} 10}
2. Access to Action
When the <action> element is configured, the method attribute is not specified. By default, the execute method in the Action class is executed.
1) Basic access
The following action access path is available on the jsp page:
1 <a href = "$ {pageContext. request. contextPath}/book/update. action"> test </a>
Struts. xml configuration file:
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 <package name="default" namespace="/book" extends="struts-default"> 4 <action name="add" class="cn.sunny.action.BookAction" method="add"> 5 <result name="success">/success.jsp</result> 6 </action> 7 <action name="update" class="cn.sunny.action.BookAction" method="update"> 8 <result name="success">/success.jsp</result> 9 </action>10 <action name="delete" class="cn.sunny.action.BookAction" method="delete">11 <result name="success">/success.jsp</result>12 </action>13 <action name="search" class="cn.sunny.action.BookAction" method="search">14 <result name="success">/success.jsp</result>15 </action>16 </package>17 </struts>
By comparing the namespace of <package> with the name attribute of <action> with the requested resource path, you can know which action to access, you can use the <action> class to know which Action class to access and the method to know which method to access.
2) use wildcards
Use wildcard * to simplify struts. xml configuration
Jsp page:
1 <a href="${pageContext.request.contextPath}/book/Book_add">book_add</a><br>2 <a href="${pageContext.request.contextPath}/book/Book_update">book_update</a><br>3 <a href="${pageContext.request.contextPath}/book/Book_delete">book_delete</a><br>4 <a href="${pageContext.request.contextPath}/book/Book_search">book_search</a><br>
Action Configuration:
1 <struts>2 <constant name="struts.devMode" value="true" />3 <package name="default" namespace="/book" extends="struts-default">4 <action name="*_*" class="cn.itcast.action.{1}Action" method="{2}">5 <result name="success">/success.jsp</result>6 </action>7 </package>8 </struts>
<Action> Each "*" in the value of the name attribute in "action" indicates any string whose length is not 0, name = "* _ *" indicates that the page access action must be named like book_add.action and book _ update. action form.
If the wildcard character is defined in the name attribute, you can use the wildcard character in the class attribute, method attribute, name attribute of <result>, and jsp page name returned, {1} represents the first *, and {2} represents the second *.
3) dynamic method call
When you do not configure the method attribute in <action> and do not want to execute the default execute method, you can use a dynamic method call. The access method is "action name" + "! "+" Method name ":
<a href="${pageContext.request.contextPath}/book/book!add">bookadd</a>
Action Configuration:
1 <struts>2 <constant name="struts.devMode" value="true" />3 <package name="default" namespace="/book" extends="struts-default">4 <action name="book" class="cn.sunny.action.BookAction">5 <result name="success">/success.jsp</result>6 </action>7 </package>8 </struts>
Note: One of the constant configurations of struts2 can be used to disable dynamic method calls. The default value is allow:
1 struts.enable.DynamicMethodInvocation = true
3. Search Order of Action names
1) obtain the URL of the Request Path. For example, the URL is:
<A href = "$ {pageContext. request. contextPath}/path1/path2/path3/update. action"> test </a>
2) First, find the package whose namespace is/path1/path2/path3. If this package exists, search for the action whose name is update in this package, if the package does not exist, perform step 1;
3) Search for the package whose namespace is/path1/path2. If this package exists, search for the action whose name is update in this package. If this package does not exist, perform step 1;
4) Find the package whose namespace is/path1. If the package exists, search for the action named update in the package. If the package still does not exist, in the package of the default namespace, find the action whose name is update (the default namespace value is "/"). If the action still cannot be found, the page prompts that the action cannot be found.
4. Default Value in package Configuration
1) if no namespace is specified for the package, the default namespace value is "/";
2) If no class is specified in action, the default class is ActionSupport;
3) If no method is specified in the action, the default method is execute;
4) if no name is specified in the result, the default name is success.