[struts]Write struts-config.xml
2007-04-10 10:08
155 查看
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>
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>
相关文章推荐
- 关于用myeclipse在struts-config.xml中配置datasource的注意事项
- struts-config.xml配置文件详解
- struts-config.xml
- Struts-config.xml配置文件讲解
- 【Struts1.2总结】struts-config.xml配置
- struts-config.xml配置3
- struts-config.xml
- struts-config.xml中的元素有严格的顺序
- Struts1.2 struts-config.xml配置详解
- The file cannot be validated as there was a connection problem. struts-config.xml
- 搭建SSH框架的时候的struts-config.xml配置文件(用controller管理)
- 【Struts1.2总结】struts-config.xml配置
- Struts-config.xml开发中遇见的问题
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- eclips编辑struts-config.xml时速度非常慢的问题
- struts-config.xml中action、forward、form-bean属性含义
- Struts-config.xml配置文件《action-mappings》元素的详解
- Struts-config.xml配置文件讲解
- web.xml /struts-config.xml 详细介绍