您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: