[Web Development Study Notes] Structs2 Result Study Notes (1) Brief Introduction
Structs2 Result Study Notes (1) Brief Introduction Question 1
<struts> <constant name="struts.devMode" value="true" /> <package name="resultTypes" namespace="/r" extends="struts-default"> <action name="r1"> <result type="dispatcher">/r1.jsp</result> </action> <action name="r2"> <result type="redirect">/r2.jsp</result> </action> <action name="r3"> <result type="chain">r1</result> </action> <action name="r4"> <result type="redirectAction">r2</result> </action> </package></struts>
Dispatcher-forward uses the server to jump to the server and display the action
Redirect-redirect to jsp
Chain server jump
RedirectAction
Question 2 chain jump-> jump from an action to another action in the package:
<package name="public" extends="struts-default"> <!-- Chain creatAccount to login, using the default parameter --> <action name="createAccount" class="..."> <result type="chain">login</result> </action> <action name="login" class="..."> <!-- Chain to another namespace --> <result type="chain"> <param name="actionName">dashboard</param> <param name="namespace">/secure</param> </result> </action></package><package name="secure" extends="struts-default" namespace="/secure"> <action name="dashboard" class="..."> <result>dashboard.jsp</result> </action></package>
The example shows that the actionName and namespace parameters are configured for the chain;
For more information, see the documentation in struts-2.1.6/docs/chain-result.html.
Question 3: global Result
// Front-end htm <body> Result type <ol> <li> <a href = "user/user? Type = 1 "> return success </a> </li> <a href =" user/user? Type = 2 "> error returned </a> </li> <a href =" user/user? Type = 3 "> return global result </a> </li> <a href =" admin/admin "> admin, inherit user package </a> </li> </ol> </body>
//struct.xml<struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <global-results> <result name="mainpage">/main.jsp</result> </global-results> <action name="index"> <result>/index.jsp</result> </action> <action name="user" class="com.struts2.user.action.UserAction"> <result>/user_success.jsp</result> <result name="error">/user_error.jsp</result> </action> </package> <package name="admin" namespace="/admin" extends="user"> <action name="admin" class="com.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package></struts>
// Class package // AdminActionpackage com. struts2.user. action; import com. opensymphony. xwork2.ActionSupport; public class AdminAction extends ActionSupport {@ Overridepublic String execute () throws Exception {return "mainpage" ;}}// UserActionpackage com. struts2.user. action; import com. opensymphony. xwork2.ActionSupport; public class UserAction extends ActionSupport {private int type; public int getType () {return type;} public void setType (int type) {this. type = type ;}@ Overridepublic String execute () throws Exception {if (type = 1) return "success"; else if (type = 2) return "error "; else return "mainpage ";}}
Analysis 1
<package name="user" namespace="/user" extends="struts-default"> <global-results> <result name="mainpage">/main.jsp</result> </global-results> <action name="index"> <result>/index.jsp</result> </action> <action name="user" class="com.struts2.user.action.UserAction"> <result>/user_success.jsp</result> <result name="error">/user_error.jsp</result> </action> </package>
In this configuration file, when the result is success, call user_success.jsp. If the result is error, call
/User_error.jsp. In all other cases, the result is configured as mainpage. This result set can be shared.
Analysis 2
<package name="admin" namespace="/admin" extends="user"> <action name="admin" class="com.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package>
Extends is used to inherit the configuration information from another package. It is used to process projects in modules. Each module has public configuration information, in this way, the public configuration information can be defined as <global-results>, and then inherited by each module to simplify processing. It seems like the inheritance relationship with c ++/java!
Problem 4: struct-default parsing source file in ReferencedLibraries/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar/struct-default.xml
<package name="struts-default" abstract="true">
The Interceptor is defined by default.
<default-interceptor-ref name="defaultStack"/><default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
Conclusion: The configuration is complex and does not need to be changed.