Struts 2 configuration file
Struts 1 uses ActionServlet as the distributor, while Struts 2 uses Filter as the distributor. If multiple filters exist, put the Struts 2 distributor Filter at the end.
Web. xml
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
Struts 2 is suffixed with. action by default. It is best to configure/* instead of/*. action, because Struts 2 integrates some Javascript and CSS resources, and these resources are not suffixed with/*. action.
Struts. properties configures some default parameters of Struts 2. Each configurable parameter has a default value. This file is not required. If you do not need to modify any parameter values, you do not need to add this file.
The default attribute of Struts 2 is located in the default. properties under the struts2-core.jar package org/apache/sturts2, and you can add struts. properties to override the default configuration under the/WEB-INF/classes project
Common attributes are as follows:
# Upload the file's working directory and the maximum size of the file struts2.multipart. saveDir = struts2.multipart. maxSize = # Struts 2 default suffix struts. action. extension = # whether the development mode is struts. devMode = false or ture # default topic, template, and template file suffix struts. ui. theme = xhtmlstruts. ui. templateDir = struts. ui. templateSuffix = ftl # Struts 2 default configuration file struts. configuration. files = struts-default.xml, struts-plugin.xml, struts. xml
Struts. xml
>
/Login. jsp
<Exception-mappings result =/exception. jsp exception = java. lang. Exception>
/Positive. jsp
/Negative. jsp
Custom Actions generally inherit from the ActionSupport class and overwrite the execute () method. Struts 2 does not have a container such as the ActionForm in Struts 1, the variable value is automatically assigned by Struts 2 through the setter method.
Import com. opensymphony. xwork2.ActionSupport; public class LoginAction extends ActionSupport {private String account; public String execute () {// main method if (......) Return LOGIN;} return SUCCESS;} // The setter and getter methods are omitted}
ActionSupport implements the Action interface, while the Action interface defines several common result names. In programming, use these preset result names as much as possible.
public interface Action{public static final String SUCCESS = success;public static final String NONE = none;public static final String ERROR = error;public static final String INPUT = input;public static final String LOGIN = login;public abstract String execute() throws Exception;}
The Action of Struts 2 does not have to implement the Action interface. Any POJO can be used as the Action. As long as this Action has the public String execute () method, Struts 2 will call the execute () method through reflection, for example
Public class LoginAction {private String account; public String execute () {if (......) Return LOGIN;} return SUCCESS;} // The setter and getter methods are omitted}
The advantage of not implementing Action is that it is not coupled with Struts 2 and the Code does not depend on the Struts 2 class library.
Execute () is the default Action method. Struts 2 can also execute other methods. as long as these methods have no parameters and return the String type, the throws declaration is optional, as shown in
Public class LoginAction extends ActionSupport {private String account; // The setter and getter methods are slightly public String execute () {// main method if (......) Return LOGIN;} return SUCCESS;} public String login () throws Exception {// custom method return LOGIN;} public String logout () throws Exception {// custom method return logout ;}}
You can use/login! Login. action to access the login () method. Similarly, use/login! Logout. action
In addition, you can configure the method to the Action.
/login.jsp
/logout.jsp
/login.jsp
/logout.jsp
The disadvantage is that multiple configurations are required.
When using POJO, Struts 1 must display a new object, while Struts 2 does not. If there is no object, it will instantiate an object through reflection at runtime.