Struts2 小结
2015-11-04 11:52
393 查看
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代码
<!-- struts2 滤镜配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,
struts-plugin.xml,
<!-- 核心struts.xml文件 -->
../conf/common/struts2/struts.xml,
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</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代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--载入默认的struts配置-->
<include file="struts-default.xml" />
<!--指定web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理
如果用户需要制定多个请求后缀,则多个后缀之间以英文逗号隔开-->
<constant name="struts.action.extension" value="action,do"></constant>
<!--设置浏览器是否缓存静态内容,默认值为true,生产环境下使用,开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false,生产环境下使用,开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--开发模式下使用,可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 动态方法调用 false为不允许 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 默认的视图主题,标签不支持label ; theme属性包括xhtml,html,simple,ajax ,默认是xhtml-->
<constant name="struts.ui.theme" value="simple"></constant>
<!--Struts2集成Spring:所有action对象由Spring来负责创建-->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 支持页面使用静态方法和属性 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 跳转到登录页 -->
<package name="CommonPackage" extends="struts-default" namespace="/common">
<action name="toLoginPage">
<result>/Web/login/page/login.jsp</result>
</action>
</package>
<!-- 指定其它的配置文件路径 -->
<include file="../conf/common/struts2/interceptor-common-exception.xml"></include>
</struts>
邢台百姓网 http://www.jinshixun.com/ mx66
少儿美术培训 http://bianmin.jinshixun.com/5685.html mx66
在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代码
<!-- struts2 滤镜配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,
struts-plugin.xml,
<!-- 核心struts.xml文件 -->
../conf/common/struts2/struts.xml,
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</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代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--载入默认的struts配置-->
<include file="struts-default.xml" />
<!--指定web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理
如果用户需要制定多个请求后缀,则多个后缀之间以英文逗号隔开-->
<constant name="struts.action.extension" value="action,do"></constant>
<!--设置浏览器是否缓存静态内容,默认值为true,生产环境下使用,开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false,生产环境下使用,开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--开发模式下使用,可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 动态方法调用 false为不允许 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 默认的视图主题,标签不支持label ; theme属性包括xhtml,html,simple,ajax ,默认是xhtml-->
<constant name="struts.ui.theme" value="simple"></constant>
<!--Struts2集成Spring:所有action对象由Spring来负责创建-->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 支持页面使用静态方法和属性 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 跳转到登录页 -->
<package name="CommonPackage" extends="struts-default" namespace="/common">
<action name="toLoginPage">
<result>/Web/login/page/login.jsp</result>
</action>
</package>
<!-- 指定其它的配置文件路径 -->
<include file="../conf/common/struts2/interceptor-common-exception.xml"></include>
</struts>
邢台百姓网 http://www.jinshixun.com/ mx66
少儿美术培训 http://bianmin.jinshixun.com/5685.html mx66
相关文章推荐
- Struts2中跳转问题
- JAVA 基础部分 运算符 及随机数
- 第六章:数据验证——深入浅出学Spring Web MVC
- 第五章:数据格式化——深入浅出学Spring Web MVC
- 第四章:数据类型转换——深入浅出学Spring Web MVC
- JAVA基础知识1
- java汉字转成拼音、拼音首字母
- java反射--如何改变private中的变量及方法
- 第三章:注解式控制器开发详解 ——深入浅出学Spring Web MVC
- 第二章:理解DispatcherServlet ——深入浅出学Spring Web MVC
- spring mvc与Mybatis整合——(三)增删改查
- 第一章:Spring Web MVC入门——深入浅出学Spring Web MVC
- springMvc 数据绑定,类型转换,数据校验 解析
- spring mvc与Mybatis整合——(二)mybatis环境搭建
- JList 双击事件
- Java中输出当前时间的各种方法
- 如何将Bitmap序列化(Bitmap的序列化问题)
- 【Java】【Flume】Flume-NG启动过程源码分析(三)
- java 写入、读取txt
- Eclipse automatically build