struts-config.xml配置

来源:互联网 发布:深圳市软件测试培训 编辑:程序博客网 时间:2024/06/05 17:15

struts-config.xml是Struts的主要配置文件,在该文件中,可以配置数据源、form-bean、action和plug-in(插件)和资源文件的信息。其文件主要结构如下所示:

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEstruts-config PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 1.2//EN""http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<data-sources>
<data-source></data-source>
</data-sources>
<form-beans>
<form-bean></form-bean>
</form-beans>
<global-forwards>
<forward/>
</global-forwards>
<action-mappings>
<action/>
</action-mappings>
<controllers></controllers>
<message-resources></message-resources>
<plug-in></plug-in>
</struts-config>

       以上各元素必须是按照这个顺序的,若开发人员打乱顺序,很可能引起Struts容器启动时出错。

       当然struts-config.xml还有<display-name/>、<description />和<icon/>子元素,因为它们用得很少,在此不再赘述。只是讲述常用的子元素的配置。

1. data-sources

本节讲述子元素data-sources的配置,该元素可以配置一个或多个data-source元素,即数据源元素,可以通过<set-property>设置driverClass、url、user、password等属性。配置实例如下:

<data-source>

                           <!-- 所用的JDBC驱动类,必须-->

                           <set-property property="driverClass"value="com.mysql.jdbc.Driver"/>

                           <!-- 所用的JDBC的URL,必须-->

                           <set-property property="url"value="jdbc:mysql://localhost/test"/>

                           <!-- 同时打开的最小连结数,缺省值为1,可选-->

                           <set-property property="minCount" value="1"/>

                           <!-- 同时打开的最大连结数,缺省值为2,可选-->

                           <set-property property="maxCount" value="5"/>

                           <!-- 连结到数据库的用户名,必须-->

                           <set-property property="user" value="root"/>

                           <!-- 连结到数据库的密码,必须-->

                           <set-property property="password" value="root"/>

                  </data-source>

开发人员还可以设置Key(绑定在ServletContext上的DataSource实例的索引键,若不设定则缺省为Action.DATA_SOURCE_KEY,如果在应用程序中有多于一个的DataSource,则必须设置Key的值)、Description(关于DataSource的描述信息)、ReadOnly(如果设为true,则表示该连结是只读的,缺省为false)、LoginTimeout(创建连结的最大允许时间,以秒为单位)和AutoCommit(如果为true,则每次execute之后会强制回滚。缺省为true)属性。

在实际项目中,例如在Hibernate+ Struts构建的系统中,一般使用Hibernate的hibernate.cfg.xml文件来配置数据源的信息。而在Hibernate+ Struts + Spring构建的系统中,一般使用spring的配置文件(eg. applicationContext.xml)来配置数据源的信息。

2. form-beans

子元素form-beans用来配置绑定到Action的各个FormBean的实例。每个FormBean实例用form-bans的子元素form-bean来定义。form-bean又分普通的FormBan和动态FormBean。

(1)普通form-bean

普通FormBean需要定义一个JavaBean类,在form-bean元素中指定该类。普通form-bean元素的定义格式如下:

<form-bean name="FormBean的名称" type="FormBean对应JavaBean类的全路径"/>

Eg.<form-bean name="UserForm"

             type="com.amigo.struts.form.user.UserForm" />

对应的FormBean类一般是继承ActionForm类,例如下面的例子定义了一个UserForm,它具有userName和password两个属性。该类的代码如下:

packagecom.amigo.struts.form.user;

importorg.apache.struts.action.ActionForm;

publicclass UserForm extends ActionForm {

        private static final long serialVersionUID = 1L;

        

        /** 用户名.*/

        private String userName;

        

        /** 密码. */

        private String password;

        public String getPassword() {

                  return password;

        }

        public void setPassword(String password) {

                  this.password = password;

        }

        public String getUserName() {

                  return userName;

        }

        public void setUserName(String userName) {

                  this.userName = userName;

        }

}

(2)动态form-bean

       动态form-bean不需要定义对应的javabean类,其元素都在struts-config.xml中定义。其type为:org.apache.struts.validator.DynaValidatorForm。下面的动态FormBean定义了userName和password属性,配置如下:

<form-bean name="UserForm"type="org.apache.struts.validator.DynaValidatorForm">

            <form-property name="userName"type="java.lang.String"/>

            <form-property name="password"type="java.lang.String"/>

</form-bean>

3 global-forwards

       global-forwards用于配置全局转发,struts首先会在<action-mappings>元素中找对应的<forward>,若找不到,则到全局转发配置中找。它包含0个或多个<forward/>元素,格式如下所示:

<forwardname="唯一的名称" path="指向资源的相对路径"/>

Eg.

<global-forwards>

                  <forward name="failed" path="/error.jsp" />

                  <forward name="success" path="/ success.jsp" />

</global-forwards>

<forward/>元素还有一个redirect属性,其默认值为false,如果redirect设为true的时候,则用HttpServletResponse.sendRedirect()方法,否则用RequestDispatcher.forward()方法,缺省为false。

4 action-mappings

       该元素用于将Action元素定义到ActionServlet类中,它含有0到多个<action/>元素,其格式如下:

<action-mappings>

<action path="Action请求的相对路径"

type="该Action的对应类的全路径"

name="该Action绑定的FormBean"

<forward name="指定处理相应请求所对应的地址"path="相对路径"/>

</action>

</action-mappings>

       每个action子元素可包含一个或多个forward子元素。除了path、type和name属性外,action还具有如下属性:

l         scope:指定ActionForm Bean的作用域(session和request),缺省为session。(可选);

l         input:当Bean发生错误时返回的路径(可选);

l         classname:指定一个调用这个Action类的ActionMapping类的全名。缺省用org.apache.struts.action.ActionMapping(可选);

l         include:如果没有forward的时候,它起forward的作用(可选);

l         validate:若为true,则会调用ActionForm的validate()方法,否则不调用,缺省为true(可选)。

forward属性也是可选的。

action元素定义举例如下:

Eg1.

<action-mappings>

<action

 path="/userAction"

 type="com.amigo.struts.action.UserAction"

 name="UserForm"

 scope="request"

 validate= "false"

 parameter="method">

            <forward name="error"path="/user/error.jsp" />

        <forward name="success" path="/user/success.jsp"/>

                  <forward name="add" path="/user/addUser.jsp"/>

                  <forward name="update" path="/user/updateUser.jsp"/>

                  <forward name="list" path="/user/userList.jsp"/>

</action>

</action-mappings>

Eg2. 有input属性的例子:

<action-mappings>

<actionpath="/calcAction"

type="com.amigo.struts.action.CalcAction"

name="CalcForm"

scope="request"

validate="true"

input="/index.jsp">

<forwardname="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>

</action>

</action-mappings>

Eg3. 仅有JSP的action元素:

<actionpath="/menu"

parameter="/default.jsp"

type="org.apache.struts.actions.ForwardAction"/>

首先,ActionServlet接到请求后调用ForwardAction的execute()方法,execute()根据配置的parameter属性值来forward到那个URI。

这样做的效果是:没有任何form被实例化,比较现实的情形可能是form在request更高级别的范围中定义;或者这个action被用作在应用程序编译好后充当系统参数,只需要更改这个配置文件而不需要重新编译系统。

5. message-resources

       该元素用来定义资源文件,格式如下:

<message-resourcesparameter="给定资源文件的全名"

classname="定义处理消息资源的类名的全名"

factory="定义MessageResourcesFactory类的全名"

key="定义绑定在这个资源包中的ServletContext的属性主键"

null=" 如果为true,则找不到消息key时,则返回null "/>

      message-resources的各属性中,只有parameter是必选的,其余都为可选,classname属性默认为:org.apache.struts.config.MessageResourcesConfig,factory属性默认为:org.apache.struts.util.property.MessageResourcesFacotry,key属性默认为:Action.MESSAGES_KEY,null属性默认为:true。

       举例如下,在struts配置文件中添加如下信息:

Eg1.<message-resources parameter="ApplicationResources" />

Eg2.<message-resources

 parameter="com.amigo.struts.ApplicationResources "

null="false"/>

6. plug-in

       该元素用于定义插件,可定义0到多个插件元素,最常见的plug-in为Struts的验证的插件,配置举例如下:

Eg1.Struts的验证的plug-in:

<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">

        <set-property property="pathnames"

                  value="/WEB-INF/validator-rules.xml, /WEB-INF/manager/validation.xml"/>

        <set-property property="stopOnFirstError" value="false"/>

</plug-in>

Eg2.Spring提供的载入插件配置:

<plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn">

<set-propertyproperty="contextConfigLocation"

            value="/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml"/>

 </plug-in>

7. 完整配置实例

       本小节举例说明struts-config.xml文件的配置:

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEstruts-config PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

 <data-sources/>

 <form-beans>

   <form-bean name="UserForm"

                  type="com.amigo.struts.form.user.UserForm" />

 </form-beans>

 

 <global-exceptions/>

 <global-forwards />

 <action-mappings>

<action

               path="/userAction"

               type="com.amigo.struts.action.UserAction"

               name="UserForm"

               scope="request"

               validate = "false"

               parameter="method" >

            <forward name="error"path="/user/error.jsp" />

                 <forwardname="success" path="/user/success.jsp"/>

                  <forward name="add" path="/user/addUser.jsp"/>

                  <forward name="update" path="/user/updateUser.jsp"/>

                  <forward name="list" path="/user/userList.jsp"/>

</action>

</action-mappings>

<message-resourcesparameter="com.amigo.struts. ApplicationResources " />

<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">

<set-propertyproperty="pathnames"

value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

<set-propertyproperty="stopOnFirstError" value="false" /> 

</plug-in>

</struts-config>