Struts2 常量配置

Struts2规定了一些特定的对整个Struts2应用起作用的常量,通过配置这些常量的值,可以改变Struts2框架的一些默认行为。

Struts2可以在三种文件中对常量进行配置:

  • struts.xml
  • struts.properties
  • web.xml

在不同配置文件中配置相同常量,会出现覆盖的情况:后一个覆盖前一个配置文件中的常量值。例如,在struts.xml中配置一个常量I,在web.xml中也配置同样的常量I,则web.xml中的常量I会覆盖struts.xml中的常量I。

struts中的部分常量

属性 说明
struts.locale 默认是en\_US,中文环境下为zh\_CN
struts.i18n.encoding 指定默认编码集,默认值UTF-8
struts.action.extension 指定需要Struts2处理的请求后缀,默认值是action,,
struts.devMode 指定Struts2是否使用开发模式,默认值false,开发时常设为true
struts.custom.i18n.resources 指定struts2所需要的国际化资源文件,用英文逗号隔开

1. struts.xml中配置常量

使用标签配置,属性有name,value


    
    
    
    
    
    
    ...省略

2. struts.properties中配置常量

该文件包含了系列的键值对key=value的形式,每个key就是一个Struts2常量名name,对应的value就是常量值value

struts.i18n.encoding=GBK

3. web.xml中配置常量

在配置Struts2的核心Filter时,通过子元素配置常量,其中元素指明常量名name,元素指明常量值value。



  struts2_4
  
  
      struts2
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      
          struts.i18n.encoding
          GBK
      
      
          struts.devMode
          true
      
  
  
      struts2
      /*
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
通常推荐在 struts.xml中配置常量,而不是在struts.properties和web.xml中配置。之所以保留struts.properties文件定义Struts2属性的方式,主要是为了保持与WebWork的向后兼容性。在实际开发中不推荐在web.xml中配置常量,因为这种配置会增加web.xml文件的内容量,降低可读性。

End...

你可能感兴趣的:(mvc,ssh,struts2)