struts2: Constant Configuration

1. 先从Apache Struts 2 Documentation下摘录一些内容:

Constants can be declared in multiple files. By default, constants are searched for in the following order, allowing for subsequent files to override previous ones:

struts-default.xml --> struts-plugin.xml --> struts.xml --> struts.properties --> web.xml

struts2中可以有以下三种方法对constent赋值

In the struts2.xml, the constant element has two required attributes: name and value.

In the struts.properties file, each entry is treated as a constant.

In the web.xml file, any FilterDispatcher initialization parameters are loaded as constants.

Constant Example (struts.xml)

  1. <struts>
  2.   <constant name="struts.devMode" value="true" />
  3.   ... 
  4. </struts>

Constant Example (struts.properties)

  1. struts.devMode = true

Constant Example (web.xml)

  1. <web-app id="WebApp_9" version="2.4" 
  2.     xmlns="http://java.sun.com/xml/ns/j2ee" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.     <filter>
  6.         <filter-name>struts</filter-name>
  7.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  8.         <init-param>
  9.             <param-name>struts.devMode</param-name>
  10.             <param-value>true</param-value>
  11.         </init-param>
  12.     </filter>
  13.     ...
  14. </web-app>

2. 我的建议是:对于constant,优先选择struts.properties文件进行配置。因为有些constant在struts.xml中配置并没有效果,具体原因我没找到。我在apache官网的论坛中找到两个issue,但也没有提供答案,现在把这两个issue贴出来,其中第一个是我遇到过的,确实没办法用struts.xml解决。

(1)struts.locale is ignored as constant in struts.xml, does work when specified in struts.properties

When specifying the "struts.locale" as <constant name="struts.locale" value="nl" /> in struts.xml, the parameter is ignored, resulting in the following warning being logged: WARN [Settings.java:143] : Settings: Could not parse struts.locale setting, substituting default VM locale

(2)When you specify the struts.locale in struts.properties, the parameter is recognized and the above warning is not logged.
The struts.custom.i18n.resources setting works from a struts.properties file, but the same setting is ignored by a constant setting in struts.xml.
struts.properties works: struts.custom.i18n.resources=resources
struts.xml does not work: <constant name="struts.custom.i18n.resources" value="resources"/>

3.struts.properties中可以配置的内容

apache官网上有全部的详细列表及解释,请上官网查看;另外也可以baidu一些常用的constant。例如

  1. ### 设置默认的locale和字符编码
  2. struts.locale=en_US:en就是english;US就是美国
  3. struts.locale=en_GB:en就是english;GB就是GreatBritain
  4. struts.i18n.encoding=UTF-8 
  5. ### 指定struts的工厂类 
  6. struts.objectFactory = spring
  7. ### 设置struts自动加载的文件列表. 
  8. strutsstruts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
  9. ### 设置要加载的国际化资源文件,以逗号分隔. 
  10. struts.custom.i18n.resourcesmessageResource1, messageResource2
  11. ### 设置当struts.xml文件改动时,是否重新加载. 
  12. struts.configuration.xml.reload = true 
  13. ### 设置struts是否为开发模式,默认为false,测试阶段一般设为true. 
  14. struts.devMode = false 
  15. ### 设置是否每次请求,都重新加载资源文件,默认值为false. 
  16. struts.i18n.reload=false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值