Struts-config.xml配置文件讲解

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" />  
   
< 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发生t误时返回的控制。(可选的) 
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    指定用来下载所用的缓存大小。缺省是4096字节。 
contentType    定义response文本类型,缺省是text/html 
Debug    定义当前系统的除错级别,缺省是0 
Locale    如果是true,则在用户的session中存放Locale对象,缺省为true 
maxFileSize    指定下载文件最大的大小。缺省是250M 
multipartClass    指定去代替org.apache.struts.upload.DiskMultipartRequestHandler类的类的全名。 
Nocache    如果是true,则会关闭每个response的缓存功能。缺省是false 
TempDir    指定上载文件所用的临时目录。缺省值由容器决定 
例子: 
① 
< 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.MessageResourcesConfig 
Factory    定义MessageResourcesFactory类的全名,缺省是 

org.apache.struts.util.property.MessageResourcesFacotry 
Key    定义绑定在这个资源包中的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.gif 
navbar.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 >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值