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)

<struts>

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

  ... 

</struts>
Constant Example (struts.properties)

struts.devMode = true
Constant Example (web.xml)

<web-app id="WebApp_9" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    ...

</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。例如

### 设置默认的locale和字符编码
struts.locale=en_US:en就是english;US就是美国
struts.locale=en_GB:en就是english;GB就是GreatBritain
struts.i18n.encoding=UTF-8 

### 指定struts的工厂类 
struts.objectFactory = spring

### 设置struts自动加载的文件列表. 
strutsstruts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml

### 设置要加载的国际化资源文件,以逗号分隔. 
struts.custom.i18n.resources= messageResource1, messageResource2

### 设置当struts.xml文件改动时,是否重新加载. 
struts.configuration.xml.reload = true 

### 设置struts是否为开发模式,默认为false,测试阶段一般设为true. 
struts.devMode = false 

### 设置是否每次请求,都重新加载资源文件,默认值为false. 
struts.i18n.reload=false


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ForWayfarer/archive/2008/10/07/3026174.aspx

你可能感兴趣的:(apache,spring,xml,Web,struts)