Struts2 小结

  • web.xml文件配置
在web.xml需要配置struts2滤镜,即当jsp向后台发送请求时需要经过struts2拦截分析处理,需要注意的是struts2与struts1和spring mvc的拦截机制不同,它是通过一个filter拦截的。
filter拦截器的类除了下面这个类以外,也可以引用“org.apache.struts2.dispatcher.FilterDispatcher”。
注意filter-mapping配置的url-pattern即拦截所有的请求,如果写成/*.action就只能拦截以.action结尾的请求。
<init-param>标签中的config指定struts2初始核心文件路径,struts.xml是最核心文件
Xml代码  收藏代码
  1. <!-- struts2 滤镜配置  -->  
  2.     <filter>  
  3.         <filter-name>struts2</filter-name>  
  4. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  5.         <init-param>  
  6.             <param-name>config</param-name>  
  7.             <param-value>  
  8.             struts-default.xml,  
  9.             struts-plugin.xml,  
  10.             <!-- 核心struts.xml文件  -->  
  11.             ../conf/common/struts2/struts.xml,  
  12.             </param-value>  
  13.         </init-param>  
  14.     </filter>  
  15.   
  16.     <filter-mapping>  
  17.         <filter-name>struts2</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
 
 
  • struts.xml文件配置
下面介绍一些struts2常用的配置信息,注意这些配置都有合适的默认值,不是必须的。
属性struts.i18n.encoding:指定字符集编码,这个配置经常用到;
属性struts.action.extension:指定JSP哪些后缀请求是struts2的请求,常用配置
属性struts.devMode:当系统发生异常,在浏览器打印详细的错误消息,产品上线时间以设为false关闭
属性struts.enable.DynamicMethodInvocation:是否允许OGNL在JSP中直接调用java方法,不推荐使用
标签include: 项目大的话,通常都会写很多struts2配置文件,然后通过include标签将其它配置文件引进来,需要注意的是如果struts.xml文件放在 src根目录下,include的内容是支持通配符的,但是如果struts.xml文件放在其它位置就不能用通配符了,必须老老实实写路径,下面这个 include就是struts.xml放在conf目录后引用其它文件的方式。
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.       
  7.     <!--载入默认的struts配置-->  
  8.     <include file="struts-default.xml" />  
  9.   
  10.     <!--指定web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->  
  11.     <constant name="struts.i18n.encoding" value="UTF-8"></constant>  
  12.       
  13.     <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理  
  14.         如果用户需要制定多个请求后缀,则多个后缀之间以英文逗号隔开-->  
  15.     <constant name="struts.action.extension" value="action,do"></constant>  
  16.       
  17.     <!--设置浏览器是否缓存静态内容,默认值为true,生产环境下使用,开发阶段最好关闭 -->  
  18.     <constant name="struts.serve.static.browserCache" value="false"></constant>  
  19.       
  20.     <!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false,生产环境下使用,开发阶段最好打开 -->  
  21.     <constant name="struts.configuration.xml.reload" value="true"></constant>  
  22.       
  23.     <!--开发模式下使用,可以打印出更详细的错误信息 -->  
  24.     <constant name="struts.devMode" value="true" />  
  25.       
  26.     <!-- 动态方法调用 false为不允许 -->  
  27.     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  28.       
  29.     <!-- 默认的视图主题,标签不支持label  ; theme属性包括xhtml,html,simple,ajax ,默认是xhtml-->  
  30.     <constant name="struts.ui.theme" value="simple"></constant>  
  31.       
  32.     <!--Struts2集成Spring:所有action对象由Spring来负责创建-->  
  33.     <constant name="struts.objectFactory" value="spring"></constant>  
  34.   
  35.     <!-- 支持页面使用静态方法和属性 -->  
  36.     <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>  
  37.       
  38.     <!-- 跳转到登录页 -->  
  39.     <package name="CommonPackage" extends="struts-default" namespace="/common">  
  40.         <action name="toLoginPage">  
  41.             <result>/Web/login/page/login.jsp</result>  
  42.         </action>  
  43.     </package>  
  44.     <!-- 指定其它的配置文件路径 -->  
  45.     <include file="../conf/common/struts2/interceptor-common-exception.xml"></include>  
  46. </struts>  
 邢台百姓网 http://www.jinshixun.com/ mx66
少儿美术培训 http://bianmin.jinshixun.com/5685.html mx66
原文地址:https://www.cnblogs.com/xinxinjiayuan/p/4935519.html