`

Struts2(16): Struts2深入探索

阅读更多

 先来看看struts2包中的一些配置文件:

1,struts-default.xml

此配置文件是struts2默认提供的,在struts2-core-2.0.14.jar包的根目录下,此配置文件提供了很多struts2默认配置的拦截器和resultType类型。用户自己编写的struts.xml配置文件可继承此配置文件,例如:

<package name="struts2" extends="struts-default">

这也类似于JAVA的子类继承父类。 上例中的name="struts2"中的名字用于定义一个包名,可用于方便其它包再继承此包,名称可以任意取。

此外,抽象包中不能有具体的action定义。

<package name="struts-default" abstract="true">

 

2,default.properties

此配置文件用于定义struts2的一些默认配置,如i18n默认支持的格式。

struts.i18n.encoding=UTF-8

 此配置文件位于struts2-core-2.0.14.jar包的org.apache.struts2包中。

此配置文件可以被重写,以下是配置文件中的原话:即在src目录下创建一个struts.properties文件。

can be overridden by a struts.properties file in the root of the classpath

要修改一个默认的配置,可以在struts.xml文件中重写,如下

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

 也可以在如上所说的struts.properties 文件中重写,建议用这种方式配置

struts.i18n.encoding=gbk

还可以在web.xml文件中配置,并且这里的优先级最高,但不建议在此处配置。

 

<filter>
          <init-param>
                   <param-name>struts.i18n.encoding</param-name>
                   <param-value>gbk</param-value>
          </init-param>
</filter>

 

此外还有一个有意思的配置,struts2的所有的action在URL地址栏中都是以.action结尾,如register.action。可以通过修改默认配置文件中的扩展名项,修改为其它格式,如修改成struts1中的.do

struts.action.extension=action

 此配置文件还定义了struts配置文件的加载顺序

### A list of configuration files automatically loaded by Struts
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml

 

 3,命名空间:

<package name="struts2" extends="struts-default" namespace="/hello">

加了命名空间后, 对应的JSP页面应修改为:

<s:form action="hello/register.action" theme="simple">

 注意:必须加.action

4,加载其它配置文件

<struts>
            <include file="struts_1.xml"></include>
            <include file="struts_2.xml"></include>

 需注意的是,struts_1.xml的写法与正常struts.xml的写法一样,应包括DTD声明等。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics