struts的核心是struts-config.xml配置文件,在这个文件里描述了所有的struts组件。在这里包括配置主要的组件及次要的组件,下面是struts-config.xml包含主要元素的内容:一、 struts-config.xml的主要元素:<?xml version=”1.0” encoding=”iso-885Array-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.sqlserverdriverurl 所用的jdbc的url(必须的)如:jdbc:microsoft:sqlserver://xg088:1433maxcount 同时打开的最大连结数,缺省值为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"/><forward name="forwardname2" path="context-relative path"/></action></action-mappings><action/>属性及其描述信息如下:属 性 描 述 信 息path 在浏览器的url中输入的字符(必须的)type 连结到本映射的action的全称(可选的)name 与本操作关联的action bean在<form-bean/>中定义name名(可选的)scope 指定actionform bean的作用域(session和request),缺省为session。(可选的)input 当bean发生错误时返回的控制。(可选的)classname 指定一个调用这个action类的actionmapping类的全名。缺省用org.apache.struts.action.actionmapping,(可选的)forward 指定处理相应请求所对应的jsp页面。(可选的)include 如果没有forward的时候,它起forward的作用。(可选的)validate 若为true,则会调用actionform的validate()方法,否则不调用,缺省为true。(可选的)例子:<action-mappings><action path="/lookupaction" type="wiley.lookupaction"name="lookupform"scope="request"validate="true"input="/index.jsp"><forward name="success" path="/quote.jsp"/><forward name="faliue" path="/index.jsp"/></action></action-mappings>七、 配置requestprocessor在struts-config.xml文件中用<controller/>子元素来定义requestprocessor,其语法格式如下:<controller processorclass="fully qualified class name" /><controller />元素属性及其描述信息如下:属 性 描 述processorclass 指定自定义的requestprocessor类的全名buffersize 指定用来下载所用的缓存大小。缺省是40Array6字节。contenttype 定义response文本类型,缺省是text/htmldebug 定义当前系统的除错级别,缺省是0locale 如果是true,则在用户的session中存放locale对象,缺省为truemaxfilesize 指定下载文件最大的大小。缺省是250mmultipartclass 指定去代替org.apache.struts.upload.diskmultipartrequesthandler类的类的全名。nocache 如果是true,则会关闭每个response的缓存功能。缺省是falsetempdir 指定上载文件所用的临时目录。缺省值由容器决定例子:① <controller processorclass="wiley.wileyrequestprocessor" />② <controller contenttype="text/html;charset=utf-8" debug="3" locale="true" nocache="true" processorclass="org.apache.struts.action.requestprocessor"/>八、 配置message resources在struts-config.xml文件中用<message-resources />元素来定义消息资源。其语法如下: <message-resources parameter="wiley.applicationresources"/><message-resources />元素属性及其描述信息如下:属 性 描 述parameter 给定资源文件全名classname 定义处理消息资源的类名的全名,缺省是org.apache.struts.config.messageresourcesconfigfactory 定义messageresourcesfactory类的全名,缺省是org.apache.struts.util.property.messageresourcesfacotrykey 定义绑定在这个资源包中的servletcontext的属性主键,缺省值是action.messages_key.null 如果为true,则找不到消息key时,则返回null,缺省是true.例子:① <message-resources parameter="wiley.applicationresources"/>② <message-resources parameter="storefrontmessageresources" null="false"/><message-resources key="image_resource_key" parameter="storefrontimageresources" null="false"/>注意:设定key的目的如下:<html:img altkey="navbar.home.image.alt" bundle="image_resource_key" pagekey="navbar.home.image" width="125" height="15" border="0"/>这里说明要到storefrontimageresources.properties资源文件中找主键值是”navbar.home.image”所对应的值。这里storefrontimageresources.properties的内容如下:……navbar.home.image=/images/home.gifnavbar.home.image.alt=home……此处navbar.home.image.alt说明的和<img alt=”home”……/>一样。九、 配置plug-in配置plug-in如下:<plug-in classname="wiley.wileyplugin"/>也可如下:<plug-in classname="com.oreilly.struts.storefront.service.memory.storefrontmemorydatabaseplugin"> <set-property property="pathname" value="/web-inf/database.xml"/></plug-in>完整配置实例
本小节举例说明struts-config.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 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" />
<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>
<message-resources parameter="com.amigo.struts. ApplicationResources " />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="false" />
</plug-in>
</struts-config>