|          
Struts的核心是struts-config.xml配置文件,在这个文件里描述了所有的Struts组件。在这里包括配置主要的组件及次要的组件,下面是struts-config.xml包含主要元素的内容:
 一、struts-config.xml的主要元素:
 <?xml version=”1.0” encoding=”ISO-8859-1”?>
 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
 "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
 <struts-config>
 
 <data-sources>
 <data-source>
 </data-source>
 </data-sources>
 
 <form-beans>
 <form-bean / >
 </form-beans>
 
 <global-forwards>
 <forward / >
 </global-forwards>
 
 <action-mappings>
 <action / >
 </action-mappings>
 
 <controller / >
 
 <message-resources / >
 
 <plug-in />
 
 </struts-config>
 注意:以上各元素的顺序是非常重要的,你的struts-config.xml配置文件必须按照这个顺序进行配置,否则在你的容器启动的时候就会出错。
 
 二、struts-config.xml的子元素:
 1.<icon / >子元素
 它包含<small-icon / >及<large-icon / >,它的作用是图形化其父元素,<small-icon/>的内容是一个16x16的图像文件,而<large-icon/>的内容是一个32x32的图像文件。如下例子:
 <icon>
 <small-icon>
 /images/smalllogo.gif
 </small-icon>
 <large-icon>
 /images/largelogo.gif
 </large-icon>
 </icon>
 2.<display-name / >子元素
 它提供对父元素的短文字(short textual)描述信息,如下:
 <display-name>
 short textual discription of its parent element
 </display-name>
 3.<description / >子元素
 它提供对父元素的完全(full-length textual)的描述信息,如下:
 <description>
 full-length textual discription of its parent element
 </description>
 4.<set-property / >子元素
 它用来设置它的父元素中设定的JavaBean的属性值,它一般用在指定的GenericDataSource 属性,扩展的ActionMappings以及扩展的 global forwards。如下:
 <set-property
 property="name of bean property"
 value="value of bean property" />
 例如:
 <set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
 <set-property property="user" value="admin"/>
 <set-property property="maxCount" value="4"/>
 <set-property property="minCount" value="2"/>
 <set-property property="password" value=""/>
 <set-property property="url" value="jdbc:mysql://localhost:3306/struts"/>
 
 三、配置JDBC数据源
 其配置形式如下:
 <data-sources>
 <data-source>
 <set-property property="driverClass" value="fully qualified path of JDBC driver"/>
 <set-property property="url" value="data source URL"/>
 <set-property property=”mincount” value="the minimum number of connections to open"/>
 <set-property property="password" value="the password used to create connections"/>
 <set-property property="user" value="the username used to create connections"/>
 </data-source>
 </data-sources>
 <data-source>的属性及其描述信息如下:
 属性描 述 信 息
 Key绑定在ServletContext上的DataSource实例的索引键,若不设定则缺省为Action.DATA_SOURCE_KEY,如果在应用程序中有多于一个的DataSource,则必须设置Key的值。
 DriverClass所用的JDBC驱动类(必须的)如:com.microsoft.jdbc.sqlserver.SQLServerDriver
 url所用的JDBC的URL(必须的)如:jdbc:microsoft:sqlserver://xg088:1433
 MaxCount同时打开的最大连结数,缺省值为2(可选的)
 MinCount同时打开的最小连结数,缺省值为1(可选的)
 User连结到数据库的用户名(必须的)
 Password连结到数据库的密码(必须的)
 Description关于DataSource的描述信息(可选的)
 ReadOnly如果设为true,则表示该连结是只读的,缺省为false。(可选的)
 LoginTimeout创建连结的最大允许时间,以秒为单位。(可选的)
 AutoCommit如果为true,则每次execute之后会强制回滚。缺省为true。(可选的)
 举例说明:
 <data-sources>
 <data-source>
 <set-property property=”key” value=” value="WILEY_DATA_SOURCE" />
 <set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
 <set-property property="url" value="jdbc:mysql://localhost/wileyusers" />
 <set-property property="maxCount" value="5"/>
 <set-property property="minCount" value="1"/>
 <set-property property="user" value="sa"/>
 <set-property property="password" value="yourpassword"/>
 </data-source>
 </data-sources>
 
 四、配置FormBean
 <form-bean / >用来定义将要绑定到Action的FormBean的实例。语法如下:
 <form-beans>
 <form-bean name="name used to uniquely identify a FormBean"
 type=”fully qualified class name of FormBean"/>
 </form-beans>
 例:
 <form-beans>
 <form-bean name="lookupForm" type="wiley.LookupForm" />
 </form-beans>
 
 五、配置全局转发
 全局转发可以定义几个<forward/>子元素,struts首先会在<action-mappings>元素中找对应的<forward>,若找不到,则到全局转发配置中找。语法如下:
 <global-forwards>
 <forward name="unique target identifier"
 path="context-relative path to targetted resource "/>
 </global-forwards>
 除了name及path属性之外,还有一个redirect属性,如果redirect设为true的时候,则用HttpServletResponse.sendRedirect()方法,否则用RequestDispatcher.forward()方法,缺省为false。
 注:如果为true,则用HttpServletResponse.sendRedirect()方法,此时存储在原来的HttpServletRequest中的值将会丢失。
 例子:
 <global-forwards>
 <forward name="success" path="/welcome.jsp"/>
 <forward name="failure" path="/index.jsp"/>
 </global-forwards>
 六、配置<action-mappings>
 它可以定义几个<action / >子元素,它主要是定义Action实例到ActionServlet类中,语法如下:
 <action-mappings>
 <action path="context-relative path mapping action to a request"
 type="fully qualified class name of the Action class"
 name="the name of the form bean bound to this Action">
 <forward name="forwardname1" path="context-relative path"/>
 
 |