Struts-config.xml details of each element of the configuration file

Source: Internet
Author: User
Struts-config.xml is the main configuration file for struts, where you can configure information for data sources, Form-bean, action, and plug-in (plug-ins) and resource files. The main structure of its file (Struts1.2 version) is as follows:

<?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>         <data-source>          </data-source>     </data-sources>          <form-beans>         </form-bean >     </form-beans>     <global-forwards>          <forward/>     </global-forwards>      <action-mappings>         <action/>      <controller/>     <message-resources/&Gt     <plug-in/> </struts-config>

Each of these elements must be in this order, and if the developer scrambles the sequence, it is likely to cause an error in the Struts container to start.

Of course Struts-config.xml also have <display-name/>, <description/> and <icon/> Sub-elements, because they are used very little, not to repeat it here. It just tells the configuration of the common child elements. 1. Data-sources

This section describes the configuration of the child element data-sources, which can be configured with one or more data-source elements, the data source element, which can be set through the <set-property> driverclass, url, user, Password and other properties. The configuration examples are as follows:

<data-source>

< The JDBC driver class used by!--, must-

<set-property property= "Driverclass" value= "Com.mysql.jdbc.Driver"/>

< the JDBC URL used by!--, must-

<set-property property= "url" value= "Jdbc:mysql://localhost/test"/>

<!--the minimum number of links open at the same time, the default value is 1, optional--

<set-property property= "Mincount" value= "1"/>

<!--the maximum number of simultaneous open links, the default value is 2, optional--

<set-property property= "MaxCount" value= "5"/>

<!--user name linked to the database, must-

<set-property property= "User" value= "root"/>

<!--password that is linked to the database, must-

<set-property property= "Password" value= "root"/>

</data-source>

The developer can also set the key (the index key of the DataSource instance bound on the ServletContext, which defaults to Action.data_source_key if not set, and if there are more than one datasource in the application, The value of the key must be set), Description (description information about DataSource), ReadOnly (if set to true, indicates that the link is read-only, false by default), LoginTimeout (the maximum allowable time to create a link, In seconds) and autocommit (if true, the rollback is forced after each execute. The default is True) property.

In a real-world project, such as a system built in Hibernate + struts, you typically use Hibernate's Hibernate.cfg.xml file to configure Data source information. In Hibernate + Struts + Spring-built systems, it is common to use spring's configuration file (eg. Applicationcontext.xml) To configure the data source information. 2. Form-beans

The child element Form-beans is used to configure the instances of each Formbean bound to the action. Each Formbean instance is defined with the child element form-bean of the Form-bans. Form-bean is also divided into ordinary formban and dynamic Formbean.

(1 ) General Form-bean

Normal Formbean needs to define a JavaBean class, which is specified in the Form-bean element. The normal Form-bean elements are defined in the following format:

<form-bean name= "Formbean's name" type= "Formbean corresponds to the full path of the JavaBean class"/>

Eg. <form-bean name= "UserForm"

Type= "Com.amigo.struts.form.user.UserForm"/>

The corresponding Formbean class is generally inherited from the Actionform class, for example, the following example defines a UserForm, which has two properties of username and password. The code for this class is as follows:

Package com.amigo.struts.form.user;

Import Org.apache.struts.action.ActionForm;

public class UserForm extends Actionform {

         private static final long serialversionuid = 1L;
         

         /** user name. */

         private String userName;         

         /** password. */

         private String password;

         Public String GetPassword () {

                   return password;

         }

         public void SetPassword (String password) {

                   this.password = password;

         }

         Public String GetUserName () {

                   return userName;

         }

         public void Setusername (String userName) {

                   this.username = userName;

         }

}

(2 ) Dynamic Form-bean

Dynamic Form-bean does not need to define the corresponding JavaBean class, whose elements are defined in Struts-config.xml. Its type is: Org.apache.struts.validator.DynaValidatorForm. The following dynamic Formbean defines the username and password properties, which are configured as follows:

<form-bean name= "UserForm" type= "Org.apache.struts.validator.DynaValidatorForm" >

<form-property name= "UserName" type= "java.lang.String"/>

<form-property name= "Password" type= "java.lang.String"/>

</form-bean> 3 Global-forwards

Global-forwards is used to configure global forwarding, struts will first find the corresponding <forward> in the <action-mappings> element, and if not found, go to the global forwarding configuration. It contains 0 or more <forward/> elements in the format as follows:

<forward name= "unique name" path= "point to relative path to resource"/>

Eg.

<global-forwards>

<forward name= "Failed" path= "/error.jsp"/>

<forward name= "Success" path= "/success.jsp"/>

</global-forwards>

The <forward/> element also has a redirect property whose default value is False, and if redirect is set to True, the Httpservletresponse.sendredirect () method is used, Otherwise, the default is false with the Requestdispatcher.forward () method. 4 action-mappings

This element is used to define the action element into the Actionservlet class, which contains 0 to multiple <action/> elements in the following format:

<action-mappings>

<action path= "Action Request relative path"

Type= "full path to the corresponding class of the action"

Name= "Formbean of this action binding"

<forward Name= "Specifies the address to handle the corresponding request Path=" relative path "/>

</action>

</action-mappings>

Each action child element can contain one or more forward child elements. In addition to the path, type, and Name properties, the action has the following properties:

L Scope: Specifies the scope (session and request) of the Actionform Bean, which is the default session. (optional);

L Input: The path returned when the Bean has an error (optional);

L ClassName: Specifies the full name of the Actionmapping class that invokes this action class. Default with org.apache.struts.action.ActionMapping (optional);

L include: If there is no forward, it plays the role of forward (optional);

L Validate: If True, the Validate () method of Actionform is called, otherwise it is not called, and the default is true (optional).

The Forward property is also optional.

An example of an action element definition is as follows:

Eg1.

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

Eg2. There is an example of the input attribute:

<action-mappings>

<action path= "/calcaction"

Type= "Com.amigo.struts.action.CalcAction"

Name= "Calcform"

Scope= "Request"

Validate= "true"

input= "/index.jsp" >

<forward name= "Success" path= "/success.jsp"/>
<forward name= "error" path= "/error.jsp"/>

</action>

</action-mappings>

Eg3. Only the action element of the JSP:

<action path= "/menu"

Parameter= "/default.jsp"

Type= "Org.apache.struts.actions.ForwardAction"/>

First, Actionservlet calls the Execute () method of Forwardaction after receiving the request, and execute () forward to that URI based on the configured parameter property value.

The effect of this is that no form is instantiated, and a more realistic scenario might be that the form is defined in a higher level of request, or that the action is used as a system parameter after the application is compiled, and only needs to change the configuration file without recompiling the system. 5. Message-resources

This element is used to define the resource file in the following format:

<message-resources parameter= "Full name of the given resource file"

Classname= "Defines the full name of the class name that handles the message resource"

Factory= "Defining the full name of the Messageresourcesfactory class"

Key= "Defines the property primary key of the ServletContext bound in this resource bundle"

Null= "If true, the message key is not found, and null is returned"/>

Of the message-resources properties, only parameter is required and the rest is optional, The ClassName property defaults to: The Org.apache.struts.config.messageresourcesconfig,factory property defaults to: Org.apache.struts.util.property.MessageResour The Cesfacotry,key property defaults to: The Action.messages_key,null property defaults to: True.

As an example, add the following information to the struts configuration file:

Eg1. <message-resources parameter= "Applicationresources"/>

Eg2. <message-resources

Parameter= "Com.amigo.struts. Applicationresources "

Null= "false"/> 6. Plug-in

This element is used to define the plug-in, which defines 0 to several plug-in elements, the most common plug-in are struts's validated plug-ins, with the following configuration examples:

Eg1. Plug-in of the verification of struts:

<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >

<set-property property= "Pathnames"

Value= "/web-inf/validator-rules.xml,/web-inf/manager/validation.xml"/>

<set-property property= "Stoponfirsterror" value= "false"/>

</plug-in>

Eg2. Spring provides the loading plug-in configuration:

<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >

<set-property property= "Contextconfiglocation"

Value= "/web-inf/applicationcontext.xml,/web-inf/action-servlet.xml"/>

</plug-in> 7. Complete Configuration Example

This section provides an example of the configuration of the Struts-config.xml file:

<?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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.