Detailed description of struts-config.xml configuration file

Source: Internet
Author: User

Struts-config.xml stuts configuration file details

(1) recently learning struts, find a struts-config.xml configuration instructions, share with you:


<Struts-config> is the root element of struts. It has eight child elements. The DTD is defined as follows:

<!ELEMENT struts-config(data-sources?,form-beans?,global-exceptions?,global-forwards?,action-mappings?,controller?,message-resources*,plug-in*)>

The above eight elements are described one by one below:

1. the date-sources element date-sources is used to configure the data source required by the application. Java provides the javax. SQL. datesource interface, which must be implemented by all data sources. The configuration is as follows:

<data-sources>  <data-source type="org.apached.commons.dbcp.BasicDataSource">    ………………  </data-source></data-sources>

The access method in the action is as follows:

javax.sql.DataSource dataSource;java.sql.Connection myConnection;try{  dataSource = getDataSource(request);  myConnection = dataSource.getConnection();}…………

For multiple data sources, the following configurations are available:

<data-sources>  <data-source key="a"  type="org.apached.commons.dbcp.BasicDataSource">    ………………  </data-source>  <data-source key="b"   type="org.apached.commons.dbcp.BasicDataSource">    ………………   </data-source></data-sources>

Access Method: datasource = getdatasource (request, "");

2. Form-beans element this element is mainly used to configure the Form Verification class. It includes the following attributes:
1. classname: generally used less. It specifies the configuration class corresponding to form-bean non-prime. The default value is Org. apache. struts. config. formbeanconfig. If custom, the formbeanconfig class must be extended. Optional.
2. Name: Unique Identifier of actionform bean. Required.
3. Type: complete Class Name of actionform. Required.
As follows:

<form-beans>  <form-bean       name="Loign"      type="com.ha.login">  </form-bean></form-beans>

For dynamic action formbean, you must configure the form-property sub-element of the form-bean element. It contains four attributes. The preceding three attributes have an initial element: Set the initial values of form fields in the form of strings. If this attribute is not set, the basic type is 0, if it is an object, it is null. Optional. As follows:

<form-beans>  <form-bean       name="Loign"      type="com.ha.login">     <form-property name="ok"  type="java.lang.String"/>     <form-property name="oks"  type="java.lang.String"/>     <form-property name="okss"  type="java.lang.Integer" initial="20"/>  </form-bean></form-beans>

3. Global-exceptions element this element mainly configures Exception Handling. Its exception sub-element represents the global exception configuration. Struts uses the configuration method to handle exceptions. It is used to set the ing between Java exceptions and exception handling classes org. Apache. Struts. Action. exceptionhandler. It has seven attributes, as shown below:
1. classname: Specifies the configuration class corresponding to the exception element. The default value is org. Apache. Struts. config. exceptionconfig. Optional.
2. Handler: Specifies the Exception Processing class. The default value is org. Apache. Struts. Action. exceptionhandler. Optional
3. Key: Specify the Message key that describes the exception in the resource bundle.
4. Path: Specify the forwarding path when an exception occurs.
5. Scope: Specifies the storage range of the actionmessages instance. Optional values include request and session. The default value is request. Optional.
6. Type: Specifies the name of the exception class to be processed. required.
7. Bundle: Specify resource bundle
As follows:

<global-exceptions>  <exception       key="global.error.invalidlogin"      path="/error.jsp"      scope="request"      type="com.hn.tree"  /></global-exceptions>

4. Global-forwards element this element is mainly used to declare the Global Forwarding relationship. It has the following four attributes:
1. classname: configuration class corresponding to the forward element. The default value is org. Apache. Struts. Action. actionforward. Optional.
2. contextrelative: If this parameter is set to true, the path attribute of the table starts with "/". The default value is false relative to the URL of the current context.
3. Name: Logical name of the forwarding path. required.
4. Path: the forwarding or Redirection URL. When contextrelative is set to false, the URL path is relative to the current application. If it is set to true, it indicates that the URL path is relative to the current context ).
5. Redirect: If this parameter is true, the redirection operation is performed. If this parameter is set to false, the redirection operation is performed. The default value is false.
As follows:
<global-forwards>  <forward  name="forms1"  path="/a.do"/>  <forward  name="forms2"  path="/nb.jsp"/><global-forwards>

5. The action-mappings element describes the ing from a specific request path to the corresponding action class. It has the following attributes:
1. attribute: Set the key of the actionform bean associated with the action within the request and session range. For example, if form bean is within the request range and this parameter is set to "mybenas", the bean instance can be returned in request. getattribute ("mybenas.
2. classsname: configuration element corresponding to the action element. The default value is org. Apache. Struts. Action. actionmapping.
3. forward: the forwarding URL path.
4. include: Specify the included URL path.
5. Input: Enter the URL path of the Form. When form verification fails, the request is forwarded to the URL.
6. Name: Specifies the name of the Action formbean associated with the action. The name must have been defined in form-bean.
7. Path: Specifies the path to access the action, which starts with "/" and has no extension.
8. Parameter: Specify the Configuration Parameter of actgion. In the execute () method of the action class, you can call the getparameter () method of the actionmapping object to read the configuration parameter.
9. roles: Specifies the security roles that are allowed to call this action. Multiple roles are separated by commas. when processing a request, requestprocessor determines whether the user has the permission to call the action based on this configuration item.
10. Scope: Specifies the scope of the actionform bean. You can select request and session. The default value is session.
11. Type: Specifies the complete Class Name of the action class.
12. Unknown: If this parameter is set to true, all invalid action URLs sent by the user can be processed. The default value is false;
13. Validate: Specifies whether to call the validate method of action formbean. The default value is true.
Note: Only one of the forward, include, and type attributes can be selected.
As follows:

<action path="/search"    type="zxj.okBean"    name="a1"    scope="request"    validate="true"    input="/b.jsp">  <forward name="tig" path="/aa.jsp"/></action>

Note: forward in this example refers to a local forwarding path. Global-forwards indicates the Global Forwarding path.

6. The controller element is used to configure actionservlet. It has the following attributes.
1. buffersize: Specifies the input buffer size of the uploaded file. Optional. The default value is 4096.
2. classname: Specifies the configuration class corresponding to the Controller element. The default value is org. Apache. Struts. config. controllerconfig.
3. conenttype: character encoding. If this parameter is set in the action and JSP pages, it is overwritten.
4. locale: Specifies whether to save the locale object to the current user's session. The default value is false.
5. processorclass: Specifies the full Java class path for the request.
6. tempdir: Specifies the temporary working directory for processing files. If this item is not set, the servlet container will be used to assign a temporary working directory to the Web application.
7. nochache: if it is true, the specific header parameters pragma, cache-control, and expise will be added to the response to prevent the page from being saved in the browser of the client. The default value is false.
As follows:

<controller  contentType="text/html;charset="UTF-8""  locale="true"  processorClass="con.ok"/>
7. The message-resources element mainly configures localized message text, which has the following attributes.
1. classname: configuration class corresponding to the message-resources element. The default value is org. Apache. Struts. config. messageresourcesconfig.
2. Factory: Specifies the factory class of the message resource. The default value is org. Apache. Struts. util. propertymessageresourcesfactory.
3. Key: Specifies the attribute key used in the servletcontext object stored in the resource bundle. By default, the string constant defined by globals. messages_key allows only one resource bundle to use the default attribute key.
4. null: specifies how the messagesources class processes the key of an unknown message. If it is true, an empty string is returned. If it is false, a related string is returned. The default value is false.
5. prameter: Specifies the resource file name of messagesources. If it is: a. B. applicationresources, the actual file path is: WEB-INF/classes/a/B/applicationresources. properties.
For example:
<message-resources null="false" parameter="defaultResource"/><message-resources key="num1" null="false" parameter="test"/>
Access:
<bean:message  key="zxj"/><bean:message  key="zxj"  bundle="num1"/>

The zxj table method is a string in the messagesource resource file.

8. Configure the struts plug-in using the Plugin-in element. The attributes are as follows:
1. classname: Specifies the struts plug-in class, which must implement the org. Apache. Struts. Action. plugiin interface. For example:


<plug-in  className="a.b.c.">  <set-property property="xxx" value="/WEB-INF/aa.xml"  /></plug-in>


Note: Please indicate the source for reprinting!



Detailed description of struts-config.xml configuration file

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.