[struts]Write struts-config.xml

  A Struts application has to have a Struts application configuration file(hereafter configuration file). As is shown in web.xml, the name and location of the configuration file can be user definable. In this example and in most other simple Struts applications, the default name of the configuration file is struts-config.xml and it typically resides under application's ./WEB-INF directory. the configuration information is then read by Struts framework when the application gets started.

  The struts-config.xml file shown below contains two categories of information:

  • Form bean definitions
  • Action mapping definitions

  A form bean definition is defined via <form-bean> element as a child of <form-beans> element. A form bean defines a JavaBean class which is used to represent the input form data that are entered by the user. Each <form-bean> element has two attributes: "name" and "type". the "name" attribute defines a logical name of the form bean and "type" attribute specifies the full path of the form bean class. the value of the "name" attribute is used in the action mapping definition. In the example shown below, the name of the bean is "submitForm" and the name of the bean calss is "submit.submitForm".

  An action mapping definition is defined via <action> element as a child of <action-mappings> element. Each <action> element defines the following:

  • Mapping between a request path and an Action class
    • Request path is specified by path attribute while Action class is specified by type attribute
    • In this struts-submit sample application, /submit.do request path is mapped to SubmitAction class, which means the Struts framework invokes the execute() method of SubmitAction class whenever it receives HTTP request that contains /submit.do as request path.
  • ActionForm object that gets passed as an argument to the execute() method of the Action class
    • It is specified by name attribute
    • In the struts-submit sample application, an instance of SubmitForm calss gets passed as an argument.
  • Page selection logic via <forward> element
    • An outcome from the execute() method of the Action class determines which page will be displayed next. In this example, a same page submit.jsp page gets displayed whether the outcome is success or failure.
  • Error page to be displayed via input attribute
    • When input validation error occurs an error page can be displayed. The error page that gets displayed is specified via input attribute. In this example, the submib.jsp page gets displayed with error message.

  Example (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 >

  
<!--  ========== Form Bean Definitions =================  -->
  
< form-beans >
    
< form-bean       name ="submitForm"  
                           type
="submit.SubmitForm" />
  
</ form-beans >

  
<!--  ========== Action Mapping Definitions ============  -->
  
< action-mappings >

    
< action    path ="/submit"
              type
="submit.SubmitAction"
              name
="submitForm"
              input
="/submit.jsp"
              scope
="request"
              validate
="true" >
             
< forward  name ="success"  path ="/submit.jsp" />           
             
< forward  name ="failure"  path ="/submit.jsp" />           
    
</ action >

  
</ action-mappings >

</ struts-config >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值