您的位置:首页 > 编程语言 > Java开发

[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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: