一、配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.propreties文件配置,是因为为了保持与WebWork的向后兼容 2.在实际开发中,在web.xml中配置常量相比其他两种,需要更多的代码量,会降低了web.xml的可读性 3.通常推荐在struts.xml文件中配置struts2的常量,而且便于集中管理 sturt2中搜索加载常量的顺序是: struts-default.xml struts-plugin.xml struts.xml sturts.propreties web.xml 注意:1.若在不同的配置文件中同时配置了相同的Struts2常量,则后一个配置文件的常量值覆盖前一个配置的常量值 |
<!-- 指定Web应用的默认编码集,相当于调用 HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory" value="spring" />
struts.serve.static.browserCache
该属性设置浏览器是否缓存静态内容。当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false。
struts.enable.DynamicMethodInvocation
该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。
struts.enable.SlashesInActionNames
该属性设置Struts 2是否允许在Action名中使用斜线,该属性的默认值是false。如果开发者希望允许在Action名中使用斜线,则可设置该属性为true。
struts.tag.altSyntax
该属性指定是否允许在Struts 2标签中使用表达式语法,因为通常都需要在标签中使用表达式语法,故此属性应该设置为true,该属性的默认值是true。
struts.devMode
该属性设置Struts 2应用是否使用开发模式。如果设置该属性为true,则可以在应用出错时显示更多、更友好的出错提示。该属性只接受true和flase两个值,该属性的 默认值是false。通常,应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false。
struts.i18n.reload
该属性设置是否每次HTTP请求到达时,系统都重新加载资源文件。该属性默认值是false。在开发阶段将该属性设置为true会更有利于开发, 但在产品发布阶段应将该属性设置为false。
提示 开发阶段将该属性设置了true,将可以在每次请求时都重新加载国际化资源文件,从而可以让开发者看到实时开发效果;产品发布阶段应该将该属性设置为 false,是为了提供响应性能,每次请求都需要重新加载资源文件会大大降低应用的性能。
struts.ui.theme
该属性指定视图标签默认的视图主题,该属性的默认值是xhtml。
struts.ui.templateDir
该属性指定视图主题所需要模板文件的位置,该属性的默认值是template,即默认加载template路径下的模板文件。
struts.ui.templateSuffix
该属性指定模板文件的后缀,该属性的默认属性值是ftl。该属性还允许使用ftl、vm或jsp,分别对应FreeMarker、 Velocity和JSP模板。
struts.configuration.xml.reload
该属性设置当struts.xml文件改变后,系统是否自动重新加载该文件。该属性的默认值是false。
struts.velocity.configfile
该属性指定Velocity框架所需的velocity.properties文件的位置。该属性的默认值为 velocity.properties。
struts.velocity.contexts
该属性指定Velocity框架的Context位置,如果该框架有多个Context,则多个Context之间以英文逗号(,)隔开。
struts.velocity.toolboxlocation
该属性指定Velocity框架的toolbox的位置。
struts.url.http.port
该属性指定Web应用所在的监听端口。该属性通常没有太大的用户,只是当Struts 2需要生成URL时(例如Url标签),该属性才提供Web应用的默认端口。
struts.url.https.port
该属性类似于struts.url.http.port属性的作用,区别是该属性指定的是Web应用的加密服务端口。
struts.url.includeParams
该属性指定Struts 2生成URL时是否包含请求参数。该属性接受none、get和all三个属性值,分别对应于不包含、仅包含GET类型请求参数和包含全部请求参数。
struts.custom.i18n.resources
该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开。
struts.dispatcher.parametersWorkaround
对于某些Java EE服务器,不支持HttpServlet Request调用getParameterMap()方法,此时可以设置该属性值为true来解决该问题。该属性的默认值是false。对于 WebLogic、Orion和OC4J服务器,通常应该设置该属性为true。
struts.freemarker.manager.classname
该属性指定Struts 2使用的FreeMarker管理器。该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是 Struts 2内建的FreeMarker管理器。
struts.freemarker.wrapper.altMap
该属性只支持true和false两个属性值,默认值是true。通常无需修改该属性值。
struts.xslt.nocache
该属性指定XSLT Result是否使用样式表缓存。当应用处于开发阶段时,该属性通常被设置为true;当应用处于产品使用阶段时,该属性通常被设置为false。
struts.configuration.files
该属性指定Struts 2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开。该属性的默认值为struts- default.xml,struts-plugin.xml,struts.xml,看到该属性值,读者应该明白为什么Struts 2框架默认加载struts.xml文件了。
struts.objectFactory
该属性指定Struts 2中的action由哪个容器创建
通常struts2加载struts2常量的顺序如下:
1. struts-default.xml:该文件保存在struts2-core-2.0.6.jar文件中。
2. struts-plugin.xml:该文件保存在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中。
3. struts.xml:该文件是Web应用默认的Struts2配置文件。
4. struts.properties:该文件是Web应用默认的Struts2配置文件。
5. web.xml:该文件是Web应用的配置文件。
如果在多个文件中配置了同一个Struts2常量,则后一个文件中的配置的常量值会覆盖前面文件中配置的常量值。
在不同文件中配置常量的方式是不一样的,但不管哪个文件中,配置Struts2常量都要指定两个属性:常量name和常量value。
推荐在struts.xml文件中配置Struts2常量。
此处只加载了前三个配置文件,这是在常量struts.configuration.files中配置的。该属性指定Struts 2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开。该属性的默认值为struts- default.xml,struts-plugin.xml,struts.xml,这就是上图中加载的三个配置文件。
Struts2常量的具体用法实例
Xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
<struts>
</struts>
<struts>
</struts>
二、Struts2配置元素说明<转载>
Struts2核心的配置文件是缺省的struts.xml。
必要的时候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自动插入应用程序,这样每个模块可以包含自己的配置文件并自动配置。在Freemarker和Velocity模块中,模板也能从classpath中加载,所以整个模块可以作为一个简单的jar文件被包含。
Struts.xml配置文件可以包含Interceptor、Action类和Results。
Struts.xml配置元素说明:
1、Packages
Packages:packages把actions、results、results types、interceptors
和interceptor-stacks组装到一个逻辑单元中,从概念上讲,packages就像一个对象,可以被其它子包从写,而且可以拥有自己独立的部分。
Name属性是packages的必填元素,它作为一个关键字被后边的包引用;extends元素是可选的,它允许包扩展一个和多个前边定义的包。注意,
struts.xml文件是至上而下处理的,所有被扩展的包,需要在扩展包前定义。
Abstract元素是可选的,它可以申明一个不包含actions的配置文件。
2、Namespace
Namespace元素把actions细分到逻辑模块,每一个namespace都有自己的
前缀(prefix),namespace避免了action之间的名字冲突,当前缀出现在URI中时,这些标签都是名字空间感知的(“namespace aware”),所以这些namespace prefix不必嵌入到表单或连接中。
Default的namespace是一个空字符串“”,如果在指定的配置文件中,没有找到action,缺省的namespace也会被查找。Local/global策略允许应用程序在action “extends”元素层次结构之外,有全局的action配置。缺省的namespace也可以不在package中申明。
Namespace prefix可以注册为java的申明式安全,以确保授权的用户才能访问namespace的资源。
Root namespace(“/”)也被支持,root就是当直接请求context path的时候的namespace。
[个人理解:namespace的用法就像struts1.x中的path一样,只不过它在package中什么路径,而在action中申明action名子罢了]
3、Include
Include元素使得框架能应用“divide and conquer”来配置文件。被include
的每个配置文件必须和struts.xml有一样的格式,一个大的项目可以采用这样方式来组织程序模块。
Include元素也可以和package交替出现,框架将按照顺序加载配置文件。
4、Interceptor configuration
Interceptor允许应用程序在Action方法执行前后定义执行代码,
Interceptor在应用程序开发中十分重要,对于Interceptor有许多用例,如validation, property population, security, logging, 和profiling。
Interceptor被定义为一个Java类,Interceptor也可以组装成Interceptor-stack,他们将按照定义的顺序执行。
在struts-default.xml中定义了一些缺省的Interceptor-stack,以便框架能很好地运行。
5、Action
Action是框架的“工作单元”。Action可以指定一个Interceptor-stack、
一序列的return type和一序列的异常处理,但只有name属性是必须的。
一:配置struts2。
首先在web.xml文件中配置filter
Xml代码
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteF
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteF
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
然后在classpath中创建struts.xml配置文件。
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value=http://blog.soso.com/qz.q/"true" />
<constant name="struts.convention.default.parent.package" value=http://blog.soso.com/qz.q/"default-package" />
<constant name="struts.convention.package.locators" value=http://blog.soso.com/qz.q/"action" />
<package name="default-package" extends="convention-default">
<default-action-ref name="index" />
<action name="index" >
<result>/WEB-INF/content/index.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value=http://blog.soso.com/qz.q/"true" />
<constant name="struts.convention.default.parent.package" value=http://blog.soso.com/qz.q/"default-package" />
<constant name="struts.convention.package.locators" value=http://blog.soso.com/qz.q/"action" />
<package name="default-package" extends="convention-default">
<default-action-ref name="index" />
<action name="index" >
<result>/WEB-INF/content/index.jsp</result>
</action>
</package>
</struts>
struts.devMode属性,配置启用调试,将有更多的错误信息输出,便于排错。struts.convention.default.parent.package属性,指定使用注解标
注的控制器的默认包。可以在这个默认包中配置全局信息。
struts.convention.package.locators属性,为查找控制器包路径的关键字。如com.mycompany.action,com.mycompany.action.user,都会被
struts2扫描。里面有继承至Action的类,或类名以Action结尾的类,都会做为Action处理。
<default-action-ref name="index" />指定了默认action,如果指定的action不存在则访问该action。
把struts2-spring-plugin-2.1.6.jar添加到classpath中,struts2会自动扫描struts-plugin.xml文件,该文件自动注册了
com.opensymphony.xwork2.ObjectFactory,完成和spring的集成。