(转)struts2.0配置文件、常量配置详解

时间:2021-06-14 10:41:28

一、配置:

在struts2中配置常量的方式有三种:

在struts.xml文件中配置

在web.xml文件中配置

在sturts.propreties文件中配置

1.之所以使用struts.propreties文件配置,是因为为了保持与WebWork的向后兼容

2.在实际开发中,在web.xml中配置常量相比其他两种,需要更多的代码量,会降低了web.xml的可读性

3.通常推荐在struts.xml文件中配置struts2的常量,而且便于集中管理

sturt2中搜索加载常量的顺序是:

struts-default.xml     
(在struts2-core-2.0.6.jar文件中)

struts-plugin.xml     
(在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中)

struts.xml            
(Web应用默认的Struts2的配置文件)

sturts.propreties      
(Web应用默认的Struts2的配置文件)

web.xml                
(Web应用下的配置文件)

注意:1.若在不同的配置文件中同时配置了相同的Struts2常量,则后一个配置文件的常量值覆盖前一个配置的常量值

<!-- 指定Web应用的默认编码集,相当于调用
HttpServletRequest的setCharacterEncoding方法
-->   
   
<constant name="struts.i18n.encoding" value="UTF-8"
/>

<!--   
该 属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即 所有匹配*.action的请求都由Struts
2处理。如 果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开
-->

<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"
/>

<!-- 该
属性指定Struts 2中的action由Spring容器创 建
-->

<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

"-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!--
指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法
-->

<constant
name="struts.i18n.encoding" value="UTF-8" />

<!--

该属性指定需要Struts
2处理的请求后缀,该属性的默认值是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" />

<!-- spring 托管 -->

<constant
name="struts.objectFactory" value="spring" />

<!--

指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration

开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。

-->

<constant
name="struts.configuration"

value="org.apache.struts2.config.DefaultConfiguration"
/>

<!-- 设置默认的locale和字符编码
-->

<constant name="struts.locale"
value="zh_CN" />

<constant
name="struts.i18n.encoding" value="GBK" />

<!-- 指定Struts的工厂类
-->

<constant
name="struts.objectFactory"
value="spring"></constant>

<!--

指定spring框架的装配模式,装配方式有: name,
type, auto, and constructor (name

是默认装配模式)

-->

<constant
name="struts.objectFactory.spring.autoWire" value="name"
/>

<!--
该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true
-->

<cosntant
name="struts.objectFactory.spring.useClassCache" />

<!-- 指定类型检查,包含tiger和notiger
-->

<cosntant
name="struts.objectTypeDeterminer" value="tiger" />

<!-- 该属性指定处理 MIME-type
multipart/form-data,文件上传 -->

<constant
name="struts.multipart.parser" value="cos" />

<constant
name="struts.multipart.parser" value="pell" />

<constant
name="struts.multipart.parser" value="jakarta" />

<!-- 指定上传文件时的临时目录,默认使用
javax.servlet.context.tempdir -->

<constant
name="struts.multipart.saveDir" value="/tmpuploadfiles"
/>

<!-- 该属性指定Struts
2文件上传中整个请求内容允许的最大字节数 -->

<constant
name="struts.multipart.maxSize" value="2097152" />

<!--

该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖

struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文

件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)

-->

<constant
name="struts.custom.properties"

value="application,org/apache/struts2/extension/custom"
/>

<!--
指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper
-->

<constant
name="struts.mapper.class"
value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper"
/>

<!-- 指定action的后缀,默认为action
-->

<constant
name="struts.action.extension" value="do" />

<!-- 被
FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true.
-->

<constant
name="struts.serve.static.browserCache" value="true"
/>

<!--
设置是否支持动态方法调用,true为支持,false不支持. -->

<constant
name="struts.enable.DynamicMethodInvocation" value="true"
/>

<!--
设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. -->

<constant
name="struts.enable.SlashesInActionNames" value="true"
/>

<!-- 是否允许使用表达式语法,默认为true.
-->

<constant
name="struts.tag.altSyntax" value="true" />

<!-- 设置当struts.xml文件改动时,是否重新加载
-->

<cosntant
name="struts.configuration.xml.reload" value="true"
/>

<!--
设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->

<cosntant name="struts.devMode"
value="true" />

<!-- 设置是否每次请求,都重新加载资源文件,默认值为false.
-->

<cosntant
name="struts.i18n.reload" value="false" />

<!--
标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->

<cosntant name="struts.ui.theme"
value="xhtml" />

<!-- 模板目录 -->

<cosntant
name="struts.ui.templateDir" value="template" />

<!-- 设置模板类型. 可以为 ftl, vm, or jsp
-->

<cosntant
name="struts.ui.templateSuffix" value="ftl" />

<!-- 定位velocity.properties 文件.
默认velocity.properties -->

<cosntant
name="struts.velocity.configfile" value="velocity.properties"
/>

<!-- 设置velocity的context.
-->

<cosntant
name="struts.velocity.contexts" value="...." />

<!-- 定位toolbox -->

<cosntant
name="struts.velocity.toolboxlocation" value="...."
/>

<!-- 指定web应用的端口 -->

<cosntant
name="struts.url.http.port" value="80" />

<!-- 指定加密端口 -->

<cosntant
name="struts.url.https.port" value="443" />

<!-- 设置生成url时,是否包含参数.值可以为:
none,get or all -->

<cosntant
name="struts.url.includeParams" value="get" />

<!-- 设置要加载的国际化资源文件,以逗号分隔.
-->

<cosntant
name="struts.custom.i18n.resources" value="application"
/>

<!--
对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(),

像 WebLogic,Orion, and
OC4J等,须设置成true,默认为false. -->

<cosntant
name="struts.dispatcher.parametersWorkaround" value="false"
/>

<!-- 指定freemarker管理器
-->

<cosntant
name="struts.freemarker.manager.classname"
value="org.apache.struts2.views.freemarker.FreemarkerManager"
/>

<!--
设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates.
-->

<cosntant
name="struts.freemarker.templatesCache" value="false"
/>

<!-- 通常不需要修改此属性.
-->

<cosntant
name="struts.freemarker.wrapper.altMap" value="true"
/>

<!-- 指定xslt
result是否使用样式表缓存.开发阶段设为true,发布阶段设为false. -->

<cosntant
name="struts.xslt.nocache" value="false" />

<!-- 设置struts自动加载的文件列表.
-->

<cosntant
name="struts.configuration.files"
value="struts-default.xml,struts-plugin.xml,struts.xml"
/>

<!--
设定是否一直在最后一个slash之前的任何位置选定namespace. -->

<cosntant
name="struts.mapper.alwaysSelectFullNamespace" value="false"
/>

</struts>

<struts>

<!-- include节点是struts2中组件化的方式
可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->

<include
file="struts-default.xml"></include>

<!--
package提供了将多个Action组织为一个模块的方式

package的名字必须是唯一的 package可以扩展
当一个package扩展自

另一个package时该package会在本身配置的基础上加入扩展的package

的配置
父package必须在子package前配置

name:package名称

extends:继承的父package名称

abstract:设置package的属性为抽象的
抽象的package不能定义action 值true:false

namespace:定义package命名空间
该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action

-->

<package name="com.kay.struts2"
extends="struts-default" namespace="/test">

<interceptors>

<!-- 定义拦截器

name:拦截器名称

class:拦截器类路径

-->

<interceptor name="timer"
class="com.kay.timer"></interceptor>

<interceptor name="logger"
class="com.kay.logger"></interceptor>

<!-- 定义拦截器栈 -->

<interceptor-stack
name="mystack">

<interceptor-ref
name="timer"></interceptor-ref>

<interceptor-ref
name="logger"></interceptor-ref>

</interceptor-stack>

</interceptors>

<!-- 定义默认的拦截器
每个Action都会自动引用

如果Action中引用了其它的拦截器 默认的拦截器将无效
-->

<default-interceptor-ref
name="mystack"></default-interceptor-ref>

<!--
全局results配置 -->

<global-results>

<result
name="input">/error.jsp</result>

</global-results>

<!-- Action配置
一个Action可以被多次映射(只要action配置中的name不同)

name:action名称

class: 对应的类的路径

method: 调用Action中的方法名

-->

<action
name="hello"
class="com.kay.struts2.Action.LoginAction">

<!-- 引用拦截器

name:拦截器名称或拦截器栈名称

-->

<interceptor-ref
name="timer"></interceptor-ref>

<!-- 节点配置

name :
result名称 和Action中返回的值相同

type :
result类型 不写则选用superpackage的type
struts-default.xml中的默认为dispatcher

-->

<result
name="success"
type="dispatcher">/talk.jsp</result>

<!--
参数设置

name:对应Action中的get/set方法

-->

<param
name="url">http://www.sina.com</param>

</action>

</package>

</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.StrutsPrepareAndExecuteFilter</filter-class>

  </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.StrutsPrepareAndExecuteFilter</filter-class>

  </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的集成。