Directory
1. Basic principles of Action... 1
2. Access Session/Application/. 1 in Action
3. Action property injection... 2
4. Action wildcard... 2
5. struts2 constant definition (request suffix) 3
6. Specify multiple struts configuration files for the application... 4
7. Action dynamic method call... 5
1. Basic principles of Action
1) when the client sends a request, the request arrives at the controller.
2) The Controller creates a ValueStack object based on the request. Each request creates an Action object, and the Action object is saved to the root stack item of the ValueStack object. Store the ValueStack object in the request. The stored key is "struts. valueStack ".
3) The Controller calls the Action object to accept the request parameters and executes the business method for processing.
4) The Controller calls the result view component based on the return value of Action for processing.
5) after the request is processed, the ValueStack object and Action object are destroyed.
2. Access Session/Application/in Action/
1) Use ActionContext to return the Map type. servlet-independent non-IoC methods can be used to retrieve objects such as requests)
In this method, the ActionContext is obtained first, and then the Map-type request and other objects are obtained.
ActionContext context = ActionContext.getContext();Map<String,Object> session = context.getSession();Map<String,Object> request = (Map<String,Object>)this.context.get("request");Map<String,Object> application =this.context.getApplication();
2) using ServletActionContext, the returned Servlet-type servlet-related non-IoC can be used to retrieve objects such as requests)
This method can be used to obtain servlet-related requests and other objects. The retrieved request object is not only used to set attributes, but can also obtain http-related information.
HttpServletRequestrequest = ServletActionContext.getRequset();HttpSessionsession = request.getSession();ServletContextapplication = ServletActionContext.getgetServletContext();
3) Implement methods for obtaining objects such as requests from unrelated IoC of the interface servlet)
Implement specific RequestAware, SessionAware, ApplicationAware and Other interfaces. The container is used to set objects such as requests.
PublicabstractclassBaseAction implementsSessionAware {// custom attribute to receive the injected SessionprivateMap <String, Object> session; // This attribute is used for subclass to obtain SessionpublicMap <String, Object> getSession () {returnsession ;} // Struts2 injects SessionpublicvoidsetSession (Map <String, Object> m) {System. out. println ("BaseAction. setSession ()... "); session = m ;}}
PublicclassLoginAction extendsBaseAction {privateAdmin admin; privateString errorMsg; publicAdmin getAdmin () {returnadmin;} publicvoidsetAdmin (Admin admin) {this. admin = admin;} publicString execute () {// 1. Check whether the input parameter is null. if (admin = null) {errorMsg = "the user name or password cannot be blank! "; Return" error ";} // 2. Obtain the account and password String code = admin. getAdminCode (); String pwd = admin. getPassword (); // 3. Check whether user data IAdminDao = DAOFactory exists based on the account and password. getAdminDAO (); Admin user = null; try {user = dao. findByCodeAndPwd (code, pwd);} catch (DAOException e) {e. printStackTrace (); errorMsg = "sorry, system error. Please contact the administrator! "; Return" error ";} // 4. Determine whether the user is null. if (user = null) {// if it is null, the account password is invalid. The username or password does not match! "; Return" error ";} else {// valid if not empty. Go to the homepage getSession (). put ("user", user); System. out. println ("user:" + getSession (). get ("user"); return "success" ;}} publicString getErrorMsg () {returnerrorMsg;} publicvoidsetErrorMsg (String errorMsg) {this. errorMsg = errorMsg ;}}
4) Implement methods for obtaining objects such as requests from the IoC related to the interface servlet)
This method can also obtain servlet-related requests and other objects. In other words, the request object retrieved in this method is not only used to set attributes, but can also obtain http-related information.
However, the retrieval method is implemented through the ServletRequestAware and ServletContextAware interfaces, that is, it is set by the struts2 container.
privateHttpServletRequest request;privateHttpSession session;privateServletContext application;publicString execute() throwsException {this.request =ServletActionContext.getRequest();this.session = this.request.getSession();this.application =ServletActionContext.getServletContext();returnSUCCESS;}
3. Action property Injection
Struts2 provides the dependency injection function for attributes in Action. In the configuration file, we can easily inject values for attributes in Action. However, attributes must provide the setter method.
Action:
publicclass ActionTest1 {privateStringsavePath;publicStringgetSavePath() {returnsavePath;}publicvoid setSavePath(StringsavePath) {this.savePath = savePath;}publicStringexecute() {return"success";}}
Struts. xml:
<Packagename = "Test5a" namespace = "/test5" extends = "struts-default"> <actionname = "list" class = "cn. action. actionTest1 "method =" execute "> <! -- Inject/images for this property --> <paramname = "savePath">/images </param> <resultname = "success">/WEB-INF/page/savePath. jsp </result> </action> </package>
The value/images is injected into the savePath attribute of Action by using the <param> node.
4. Action wildcard
Publicclass UserAction {// simulate the New user Function publicString add () {System. out. println ("new user simulation... "); return" success ";} // simulate modifying publicString modify () {System. out. println ("simulate user modification... "); return" success ";} publicString query () {System. out. println ("simulate user query... "); return" success ";}}
<struts><packagename="user"namespace=""extends="struts-default"><actionname="*_*_*"class="com.tarena.action.{1}Action"method="{2}"><resultname="success">/WEB-INF/jsp/{3}.jsp</result></action></package></struts>
5. struts2 constant definition (request suffix)
1), Request suffix
Struts2 uses the. action suffix to access Action by default. In fact, the default suffix can be a constant
"Struts. action. extension" is modified. For example, we can configure Struts2 to process only the request paths suffixed with. do:
<Struts>
<Constant name = "struts. action. extension" value = "do"/>
</Struts>
If you need to specify multiple request suffixes, multiple suffixes are separated by commas. For example:
<Constantname = "struts. action. extension" value = "do, go"/>
2), Constant definition:
Constants can be configured in struts. xml or struts. properties. We recommend that you configure them in struts. xml. The two configuration methods are as follows:
Configure constants in the struts. xml file
<Struts>
<Constant name = "struts. action. extension" value = "do"/>
</Struts>
Configure the constant struts. action. extension = do in struts. properties.
Since constants can be defined in the following configuration files, we need to understand the search sequence of struts2 loading constants:
Struts-default.xml
Struts-plugin.xml
Struts. xml
Struts. properties
Web. xml
If the same constant is configured in multiple files, the constant value configured in the next file overwrites the constant value configured in the previous file.
3), Constant introduction:
<! -- Specify the default sequence set, which acts on the setCharacterEncoding method of HttpServletRequest and the output of freemarker and velocity --> <constantname = "struts. i18n. encoding" value = "UTF-8"/> <! -- This attribute specifies the request suffix to be processed by Struts 2. The default value of this attribute is action, that is, all requests matching *. action are processed by Struts2. If you need to specify multiple request suffixes, multiple suffixes are separated by commas. --> <Constantname = "struts. action. extension" value = "do"/> <! -- Sets whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it during development --> <constantname = "struts. serve. static. browserCache "value =" false "/> <! -- When the struts configuration file is modified, whether the system automatically reloads the file. The default value is false (used in the production environment). It is best to open the file during development --> <constantname = "struts. configuration. xml. reload "value =" true "/> <! -- Used in development mode to print more detailed error information --> <constantname = "struts. devMode" value = "true"/> <! -- Default view topic --> <constantname = "struts. ui. theme" value = "simple"/> <! -When integrating with spring, specify that spring is responsible for creating action objects --> <constantname = "struts. objectFactory" value = "spring"/> <! -This attribute sets whether Struts 2 supports dynamic method calls. The default value of this attribute is true. To disable dynamic method calling, set this attribute to false. --> <Constantname = "struts. enable. DynamicMethodInvocation" value = "false"/> <! -- Size limit of uploaded files --> <constantname = "struts. multipart. maxSize" value = "10701096"/>
6. Specify multiple struts configuration files for the Application
<Struts>
<Shortdefile = "struts-user.xml"/>
<Shortdefile = "struts-order.xml"/>
</Struts>
7. Dynamic Action method call
If there are multiple methods in the Action, we can use it! + The method name calls the specified method. As follows:
PublicclassHelloWorldAction {privateString message; publicString execute () throwsException {this. message = "My first struts2 application"; return "success";} publicString other () throwsException {this. message = "second method"; return "success ";}}
Assume that the URL path for accessing the preceding action is/struts/test/helloworld. action.
To access the action's other () method, we can call it like this:
/Struts/test/helloworld! Other. action
If you do not want to use dynamic method calls, you can use the constant struts. enable. DynamicMethodInvocation to disable dynamic method calls.
<Constant name = "struts. enable. DynamicMethodInvocation" value = "false"/>
This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1287189