`

struts2: Constant Configuration

    博客分类:
  • JAVA
阅读更多
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
分享到:
评论

相关推荐

    Struts2\constant应用

    ### Struts2 Constant 配置详解 #### 一、引言 在Struts2框架中,`constant`配置项主要用于设定框架的一些基本行为和优化选项。这些配置项可以帮助开发者更好地控制Struts2的行为,比如国际化编码设置、请求处理...

    struts2核心配置文件

    - **struts.configuration**:指定加载Struts2配置文件的配置文件管理器,默认值为 `org.apache.struts2.config.DefaultConfiguration`。如果需要自定义配置管理器,可以通过实现 `Configuration` 接口来自定义类。...

    struts2初始使用环境配置

    &lt;constant name="struts.devMode" value="true" /&gt; &lt;!-- 开发模式,便于调试 --&gt; &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;result name="success"&gt;/HelloWorld.jsp &lt;/struts&gt; ``...

    struts2配置2.5版

    &lt;constant name="struts.configuration.xml.reload" value="true" /&gt; 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键--&gt;properties--&gt;java Source Attachment--&gt;External ...

    struts2配置文件

    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt; &lt;struts&gt; &lt;constant name="struts.i18n.encoding" value="UTF-8" /&gt; &lt;package name="...

    struts2 配置dtd 自动提示

    例如,当你在`&lt;struts&gt;`元素下键入`时,Eclipse会列出所有可能的子元素,如`&lt;bean&gt;`, `&lt;constant&gt;`, `&lt;include&gt;`, `&lt;package&gt;`等。按`Alt+?`键可在当前元素内部获取属性提示,包括属性的必需性和可选值。 请注意,...

    Maven实现struts2注解

    &lt;constant name="struts.objectFactory" value="struts注解工厂类全限定名,比如:org.apache.struts2.spring.StrutsSpringObjectFactory" /&gt; &lt;package name="default" extends="struts-default"&gt; ...

    struts2开发遇到的问题

    &lt;constant name="struts.locale" value="en_GB" /&gt; ``` 2. **警告:No configuration found for the specified action: 'sum.action' in namespace: ''** 这个警告通常是因为Struts2找不到指定的Action。如果在...

    Struts2框架笔记

    &lt;constant name="struts.devMode" value="true"/&gt; &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;result name="success"&gt;/WEB-INF/content/hello.jsp &lt;/struts&gt; ``` #### 标签详解...

    struts2对AJAX的支持

    Struts2是一个强大的Java web框架,它为开发者提供了丰富的功能,包括对AJAX(Asynchronous JavaScript and XML)的优秀支持。在Web开发中,AJAX技术允许页面在不刷新整个页面的情况下与服务器进行异步通信,提升了...

    struts2库的导入

    &lt;constant name="struts.enable.S2-Dispatcher.headers" value="false" /&gt; &lt;package name="default" extends="struts-default"&gt; &lt;result name="success"&gt;/success.jsp &lt;/struts&gt; ``` 这个例子定义了一个...

    MyFramework - struts2 零配置:convention

    Struts2是一个非常流行的Java Web框架,用于构建可维护性和扩展性高的企业级应用程序。"MyFramework - Struts2 零配置:convention" 主题着重于介绍如何使用Struts2的Convention插件实现“零配置”开发,极大地简化...

    Struts 2中的constant配置详解

    在Struts 2中,`constant`配置元素用于定义和改变框架的行为,允许开发者根据项目需求调整Struts 2的核心设置。下面我们将详细探讨这些配置属性的作用。 1. **struts.i18n.encoding**: 这个属性定义了Web应用的默认...

    Struts2手动搭建所有的jar包及相应的struts.xml和web.xml

    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"&gt; &lt;struts&gt; &lt;constant name="struts.enable.DynamicMethodInvocation" value="false" /&gt; ...

    ·Struts2配置文件介绍 超级详细

    - **Interceptor configuration**:在Struts2中,拦截器是非常重要的组成部分,它们负责在Action执行前后进行一系列的处理操作,如验证、日志记录、性能监控等。Struts2提供了一系列内置的拦截器,如`validation`、`...

    java8+tomcat8+struts2.5+spring4.3+hibernate5.2框架搭建详细过程

    &lt;constant name="struts.devMode" value="true"/&gt; &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;result&gt;/helloWorld.jsp &lt;/struts&gt; ``` **2. web.xml**: - 配置Struts2的过滤器...

    struts2属性文件struts.xml的配置

    DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"&gt; ``` - **XML版本声明**:指明了XML文档的版本号和编码方式。 - **...

    Struts2入门级别文档

    - `&lt;constant name="struts.configuration.xml.reload" value="true"/&gt;`:控制配置文件修改后是否自动重新加载,默认为`false`,适用于生产环境;开发阶段推荐设置为`true`。 - `&lt;constant name="struts.ui.theme" ...

    搭建EXTJS和STRUTS2框架(ext和struts2简单实例)

    ### 搭建EXTJS和STRUTS2框架(ext和struts2简单实例) #### 一、概述 本文档将详细介绍如何在Java Web项目中搭建EXTJS和STRUTS2框架,并通过一个简单的实例来展示如何使这两个技术协同工作。EXTJS是一个用于构建交互...

    struts2配置文件以及代码示例

    &lt;constant name="struts.devMode" value="true"/&gt; &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;result name="success"&gt;/WEB-INF/jsp/welcome.jsp &lt;result name="success"&gt;/...

Global site tag (gtag.js) - Google Analytics