Detailed description of the Struts2 configuration file

Source: Internet
Author: User

Detailed description of the Struts2 configuration file
Introduction:

Several configuration files related to Struts2 are commonly used, such as struts. properties, web. xml, and struts. xml. Configure the Struts2 distributor Filter in web. xml. Configure SOME properties OF Struts2 in struts. properties. Struts. xml is configured with Struts2 Action. Struts2: http://struts.apache.org/

Struts. properties

Struts. properties configures some Struts2 parameters. Each configurable parameter has a default value. This file is not required. If you do not need to change any parameter value, you do not need to add this file.

Common attributes that need to be reconfigured in struts. properties include:

// Specify the default locale set, for the request parameter with Chinese should be set Chen GBK or GB2312. default UTF-8struts.i18n.encoding = GB2312 // whether each HTTP request arrives, the international resource file is reloaded. The default value is falsestruts. i18n. reload = true // whether to reload the file after struts. xml is changed. We recommend that you set this attribute to "true" during development to improve development efficiency. The default value is falsestruts. configuration. xml. reload = true // whether the development mode of Struts2 is used. More error messages can be obtained for debugging. Set this parameter to true during development. Default Value: falsestruts. devMode = true // you can specify whether the browser caches static pages. Set false in the development phase to obtain the latest response from the server. The default value is truestruts. serve. static. browserCache = true // requests with the specified suffix. action can be processed by Struts2. You can configure multiple request suffixes, such. do ,. struts2, etc. multiple suffixes are separated by commas (,) during configuration. action. extension = action, do, struts2, // configure the port number when the server is running. Generally, this attribute is not modified. If the port number is occupied, the port number is reassigned. The default value is 80struts. url. http. port = 8080.

 

Note: The default properties of Struts2 are located in the default. properties under the struts2-core-2.0.11.1.jar package org/apache/struts2, and you can add struts. properties to override the default configuration under the project's WEB-INF/class.

Web. xml

Any MVC framework needs to be integrated with Web applications. This requires the help of the web. xml file. Only the Servlet configured in the web. xml file will be loaded. Generally, all MVC frameworks require the Web application to load a core controller. For the Struts2 framework, the StrutsPrepareAndExecuteFiter needs to be loaded. The Filter will load the Struts2 Framework of the application.

In addition, you can configure the Struts2 constant in Web. xml (the struts. properties file is used to configure constants ).

The code for configuring StrutsPrepareAndExecuteFiter is as follows:

<filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

Note: The above code snippet exists in web. xml in struts2-blank \ WEB-INF

Struts. xml

Struts. xml is the core configuration file of Struts2. It configures Action, JSP, Exception, Intercepter, and so on. In addition, the configuration in struts. properties can also be configured in struts. xml.

A complete struts. xml configuration example is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Http: // localhost: 8081/systsys/test/demo. do 1. strutsPrepareAndExecuteFilter according to struts. xml configuration file information, determine all *. all do-end requests are processed by struts. 2. once found. the do end request appears and is intercepted directly. do the following:. http: // localhost: 8081/systsys/test/demo (remove identification flag) B. /test/demo (get the real resource access address information) c. /test to obtain the Directory and match the namespace d of the corresponding package. the demo obtains the action name and searches for it in the corresponding package. if it finds the action, it creates an instance of the action and calls its execute method once. --> <! -- Set it to the development mode, and the system running speed will be reduced, but the error prompt provided is more intuitive. It is generally used for system debugging --> <constant name = "struts. devMode "value =" true "/> <constant name =" struts. action. extension "value =" do "/> <! -- All *. all the requests ending with do are processed by struts. If no suffix is explicitly specified for action identification, the default value is action, such as toInput. action --> <constant name = "struts. ui. theme "value =" simple "> </constant> <! -- Struts2 automatically generates web pages for programmers and provides multiple generation templates --> <constant name = "struts. i18n. encoding" value = "UTF-8"> </constant> <! -- Package is a group of several actions with relevant functions --> <package name = "demoPak" namespace = "/test" extends = "struts-default"> <action name =" demo "class =" edu. fjnu. specified sys. action. demoAction "> </action> </package> <package name =" your sys-default "namespace ="/"extends =" struts-default "> <interceptors> <interceptor name = "authentication" class = "edu. fjnu. specified sys. interceptor. authenticationInterceptor "> </interceptor> <! -- Common actions that require permissions are common actions that can be used only when authentication is required, the user interceptor stack will be used --> <interceptor-stack name = "user"> <interceptor-ref name = "authentication"/> <interceptor-ref name = "defaultStack"/> </interceptor-stack> <! -- An action that can be used only after authentication is required for the submission of user forms that require permissions. In addition, this action has the ability to express the submission operation, the user-submit interceptor stack will be used --> <interceptor-stack name = "user-submit"> <interceptor-ref name = "user"/> </interceptor-stack> <! -- The action that can be used only after authentication is required. In addition, this action has an expression submission operation and the file submission function. The user-file-submit interceptor is used.
Stack --> <interceptor-stack name = "user-file-submit"> <interceptor-ref name = "fileUpload"> <param name = "allowedTypes"> image/bmp, image/pjpeg, image/png </param> <param name = "maximumSize"> 200000 </param> </interceptor-ref> <interceptor-ref name = "user"/> </interceptor -stack> <! -- Actions that can be accessed without any identity requirements for access with non-permission information, the guest interceptor stack will be used --> <interceptor-stack name = "guest"> <interceptor-ref name = "defaultStack"/> </interceptor-stack> </interceptors> <! -- No special action is specified for the interceptor, the user interceptor stack is used by default --> <default-interceptor-ref name = "user"/> <global-results> <result name = "gotoLoginAction" type = "redirectAction"> <param name = "actionName"> gotoLogin </param> <param name = "namespace">/security </param> </result> <result name = "error" type = "redirect ">/error. jsp </result> </global-results> </package> <include file = "security-module.xml"/> <include file = "room-module.xml"/> <include file = "hotel-module.xml" /> <include file = "user-module.xml"/> </struts>

 

Note: The default location of the struts. xml configuration file is under the struts2-blank package/WEB-INF/calsses/struts. xml. You can place struts. xml in the src folder of the MyEclipse project. Log4j2. xml is also in this directory.

Configure Struts2 constant

There are three ways to configure Struts2 constants:

1. Configure constants in the struts. properties file.

2. When configuring the core Filter in the web. xml file, the constant is configured through initialization parameters.

3. Use the <constant.../> element in the struts. xml file to configure constants.

Generally, the Struts2 framework loads the Struts2 constant in the following search order:

1. struts-default.xml: this file is saved in the struts2-core-2.3.1.2.jar file.

2. struts-plugin.xml: this file is saved in the struts2-xxx-2.3.1.2.jar file.

3. struts. xml: The struts2 configuration file of the Web application.

4. struts. properties: this file is the default Struts2 configuration file for Web applications.

5. web. xml: the configuration file of the Web application.

Note: The order in which the Struts2 framework searches for Struts2 constants is specified. However, if the same Struts2 constant is configured in multiple files, the constant value configured in the subsequent file overwrites the constant value configured in the previous 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.