Struts 1使用ActionServlet作为分发器,而Struts2使用Filter作为分发器。如果有多个Filter,要把Struts 2的分发器Filter放在最后
web.xml
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Struts 2的默认后缀为".action",配置<url-pattern>时最好配置为"/*"而不要只配置成"/*.action",因为Struts 2集成了一些Javascript、CSS资源,而这些资源都不以"/*.action"为后缀
struts.properties配置了Struts 2的一些默认参数,每个可配置的参数都有一个默认值,该文件不是必须的,如果无需修改任何参数值,可以不用添加该文件
Struts 2的默认属性位于struts2-core.jar包org/apache/sturts2下面的default.properties里,用户可以在项目的/WEB-INF/classes下添加struts.properties覆盖默认的配置
常用属性如下
- #上传文件的工作目录与文件的最大尺寸
- struts2.multipart.saveDir=
- struts2.multipart.maxSize=
- #Struts 2的默认后缀
- struts.action.extension=
- #是否为开发模式
- struts.devMode =false or ture
- #默认的主题、模板、模板文件后缀
- struts.ui.theme= xhtml
- struts.ui.templateDir=
- struts.ui.templateSuffix= ftl
- #Struts 2的默认配置文件
- struts.configuration.files= struts-default.xml,struts-plugin.xml,struts.xml
#上传文件的工作目录与文件的最大尺寸
struts2.multipart.saveDir=
struts2.multipart.maxSize=
#Struts 2的默认后缀
struts.action.extension=
#是否为开发模式
struts.devMode =false or ture
#默认的主题、模板、模板文件后缀
struts.ui.theme= xhtml
struts.ui.templateDir=
struts.ui.templateSuffix= ftl
#Struts 2的默认配置文件
struts.configuration.files= struts-default.xml,struts-plugin.xml,struts.xml
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache SoftwareFoundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
- <constant name="struts.action.extension" value="do" />
- <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
- <constant name="struts.serve.static.browserCache" value="false" />
- <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
- <constant name="struts.configuration.xml.reload" value="true" />
- <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
- <constant name="struts.devMode" value="true" />
- <!-- 默认的视图主题 -->
- <constant name="struts.ui.theme" value="simple" />
- <!--<constant name="struts.objectFactory" value="spring" />-->
- <!--解决乱码-->
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M)-->
- <constant name="struts.multipart.maxSize" value="10701096"/>
- <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir-->
- <constant name="struts.multipart.saveDir" value="d:/tmp" />
- <!--将其他配置文件包含进来,其他配置文件必须是完整的-->
- <include file="struts-user.xml"></include>
- <include file="struts-login.xml"></include>
- <!--配置Bean-->
- <bean class="" name=""></bean>
- <!—自定义的package一般继承自struts-default,struts-default定义了默认的拦截器,比如从request中获取数据并设置到Action上的工作都是Struts2拦截器完成的,如果配置了namespace ,那么在访问时需要加上namespace -->
- <package name="struts2" namespace="/mystruts" extends="struts-default">
- <!--配置拦截器-->
- <interceptors>
- <interceptor name="loginInterceptor" class="com.clf.LoginInterceptor"></<interceptors>>
- </interceptors>
- <!--配置全局result-->
- <global-results>
- <result name="login">/login.jsp</result>
- </global-results>
- <!--配置全局异常-->
- <global-exception-mappings>
- < exception-mappingsresultexception-mappingsresult="/exception.jsp" exception="java.lang.Exception"> </ exception-mappings >
- </global- exception-mappings>
- <!--配置action-->
- <action name="sum" class="action.FirstAction">
- <result name="positive">/positive.jsp</result>
- <result name="negative">/negative.jsp</result>
- </action>
- </package>
- </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解决乱码-->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M)-->
<constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir-->
<constant name="struts.multipart.saveDir" value="d:/tmp" />
<!--将其他配置文件包含进来,其他配置文件必须是完整的-->
<include file="struts-user.xml"></include>
<include file="struts-login.xml"></include>
<!--配置Bean-->
<bean class="" name=""></bean>
<!—自定义的package一般继承自struts-default,struts-default定义了默认的拦截器,比如从request中获取数据并设置到Action上的工作都是Struts2拦截器完成的,如果配置了namespace ,那么在访问时需要加上namespace -->
<package name="struts2" namespace="/mystruts" extends="struts-default">
<!--配置拦截器-->
<interceptors>
<interceptor name="loginInterceptor" class="com.clf.LoginInterceptor"></<interceptors>>
</interceptors>
<!--配置全局result-->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<!--配置全局异常-->
<global-exception-mappings>
< exception-mappingsresult="/exception.jsp" exception="java.lang.Exception"> </ exception-mappings >
</global- exception-mappings>
<!--配置action-->
<action name="sum" class="action.FirstAction">
<result name="positive">/positive.jsp</result>
<result name="negative">/negative.jsp</result>
</action>
</package>
</struts>
自定义Action一般直接继承自ActionSupport类,并覆盖execute()方法,Struts 2没有Struts 1中的ActionForm之类的容器,此类容器由Action兼任,变量的值会被Struts2通过setter方法自动赋值
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport{
- private String account;
- private String account;
- public String execute(){ //主方法
- if(……)
- return LOGIN;
- }
- return SUCCESS;
- }
- //setter、getter方法略
- }
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String account;
private String account;
public String execute(){ //主方法
if(……)
return LOGIN;
}
return SUCCESS;
}
//setter、getter方法略
}
ActionSupport实现了Action接口,而Action接口中定义了几个常用的result名称,编程中尽量使用这些预置的result名称
- 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() throwaException;
- }
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() throwaException;
}
Struts 2的Action不一定非要实现Action接口,任何POJO都可以用作Action,只要这个Action具有publicString execute()方法,Struts 2会通过反射调用execute()方法,如
- public class LoginAction {
- private String account;
- private String account;
- public String execute(){
- if(……)
- return LOGIN;
- }
- return SUCCESS;
- }
- //setter、getter方法略
- }
public class LoginAction {
private String account;
private String account;
public String execute(){
if(……)
return LOGIN;
}
return SUCCESS;
}
//setter、getter方法略
}
不实现Action的好处是不与Struts2发生耦合,代码不依赖Struts 2的类库
execute()是Action的默认方法,Struts2还可以执行其他方法,只要这些方法没有参数,并返回String类型,throws声明可有可无,如
- public class LoginAction extends ActionSupport{
- private String account;
- private String account;
- //setter、getter方法略
- public String execute(){ //主方法
- if(……)
- return LOGIN;
- }
- return SUCCESS;
- }
- public String login() throwsException{ //自定义方法
- return LOGIN;
- }
- public String logout() throwsException{ //自定义方法
- return "logout";
- }
- }
public class LoginAction extends ActionSupport{
private String account;
private String account;
//setter、getter方法略
public String execute(){ //主方法
if(……)
return LOGIN;
}
return SUCCESS;
}
public String login() throwsException{ //自定义方法
return LOGIN;
}
public String logout() throwsException{ //自定义方法
return "logout";
}
}
可以通过/login!login.action访问login()方法,同理,通过/login!logout.action访问logout()方法
此外,也可以把方法配置到Action当中
- <action name="login" class="com.clf.LoginAction" method="login">
- <result name="login">/login.jsp</result>
- <result name="logout">/logout.jsp</result>
- </action>
- <action name="logout" class="com.clf.LoginAction" method="logout">
- <result name="login">/login.jsp</result>
- <result name="logout">/logout.jsp</result>
- </action>
<action name="login" class="com.clf.LoginAction" method="login">
<result name="login">/login.jsp</result>
<result name="logout">/logout.jsp</result>
</action>
<action name="logout" class="com.clf.LoginAction" method="logout">
<result name="login">/login.jsp</result>
<result name="logout">/logout.jsp</result>
</action>
缺点是需要配置多次
Struts 1在使用POJO时,必须显示的new一个对象,而Struts2不需要,如果没有对象,会在运行时通过反射实例化一个对象