Detailed description of struts2.0 configuration file and constant Configuration

Source: Internet
Author: User
Tags i18n

Detailed description of struts2.0 configuration file and constant Configuration

Generally, struts2 loads struts2 constants in the following order: 1. struts-default.xml: The file is saved in the struts2-core-2.0.6.jar file.
2. struts-plugin.xml: this file is saved in struts2 plug-in jar files such as struts2-Xxx-2.0.6.jar.
3. Struts. xml: this file is the default struts2 configuration file for Web applications.
4. Struts. properties: this file is the default struts2 configuration file for Web applications.
5. Web. xml: the configuration file of the Web application.

If the same struts2 constant is configured in multiple files, the constant value configured in the latter file overwrites the constant value configured in the previous file.
The configuration of constants in different files is different, but in any file, the struts2 constant must be configured with two attributes: constant name and constant value.

We recommend that you configure the struts2 constant in the Struts. xml file.

Only the first three configuration files are loaded here, which is configured in the constant struts. configuration. Files. This attribute specifies the default configuration file loaded by the struts2 framework. If you need to specify that multiple configuration files are loaded by default, the names of multiple configuration files are separated by commas. The default value for this property is struts-default.xml, struts-plugin.xml, Struts. XML, which is the three configuration files that are loaded.

Usage example of struts2 Constants

XML Code

<? Xmlversion = "1.0" encoding = "UTF-8"?>

<! Doctypestrutspublic

"-// Apachesoftwarefoundation // dtdstrutsconfiguration2.0 // en"

Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>

<! -- Specify the default sequence set of the Web application, which is equivalent to calling the setcharacterencoding method of httpservletrequest -->

<Constant name = "struts. i18n. encoding" value = "UTF-8"/>

<! --

This attribute specifies the request suffix to be processed by struts2. The default value of this attribute is action, that is, all requests matching *. Action are processed by struts2.

If you need to specify multiple request suffixes, multiple suffixes are separated by commas.

-->

<Constant name = "struts. Action. Extension" value = "do"/>

<! -- Set whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it during development. -->

<Constant name = "struts. Serve. Static. browsercache" value = "false"/>

<! -- When the struts configuration file is modified, whether the system automatically reloads the file. The default value is false (used in the production environment). It is best to open the file during development. -->

<Constant name = "struts. configuration. xml. Reload" value = "true"/>

<! -- Used in development mode to print more detailed error information -->

<Constant name = "struts. devmode" value = "true"/>

<! -- Default view topic -->

<Constant name = "struts. UI. Theme" value = "simple"/>

<! -- Spring hosting -->

<Constant name = "struts. objectfactory" value = "Spring"/>

<! --

Specify to load the struts2 Configuration File Manager. The default value is org. Apache. struts2.config. defaultconfiguration.

You can customize the configuration file manager. To implement the configuration interface, you can automatically load the struts2 configuration file.

-->

<Constant name = "struts. Configuration"

Value = "org. Apache. struts2.config. defaultconfiguration"/>

<! -- Set the default locale and character encoding -->

<Constant name = "struts. locale" value = "zh_cn"/>

<Constant name = "struts. i18n. encoding" value = "GBK"/>

<! -- Specify the struts factory class -->

<Constant name = "struts. objectfactory" value = "Spring"> </constant>

<! --

Specify the Assembly mode of the Spring framework. The assembly methods include name, type, auto, andconstructor (name

Is the default Assembly Mode)

-->

<Constant name = "struts. objectfactory. Spring. autowire" value = "name"/>

<! -- This attribute specifies whether to cache bean when spring is integrated. The value is Trueor/false. The default value is true. -->

<Constant name = "struts. objectfactory. Spring. useclasscache"/>

<! -- Specify the type check, including tiger and notiger -->

<Constant name = "struts. objecttypedeterminer" value = "tiger"/>

<! -- This attribute specifies the processing of mime-typemultipart/form-data, File Upload -->

<Constant name = "struts. multipart. parser" value = "Cos"/>

<Constant name = "struts. multipart. parser" value = "Pell"/>

<Constant name = "struts. multipart. parser" value = "Jakarta"/>

<! -- Specifies the temporary directory when the file is uploaded. By default, javax. servlet. Context. tempdir -->

<Constant name = "struts. multipart. savedir" value = "/tmpuploadfiles"/>

<! -- This attribute specifies the maximum number of bytes allowed for the entire request content during the struts2 File Upload -->

<Constant name = "struts. multipart. maxsize" value = "2097152"/>

<! --

This attribute specifies that the struts2 application loads User-Defined Property files. The specified properties of this custom property file will not be overwritten.

The attribute specified in the Struts. properties file. If you need to load multiple custom attribute files, multiple custom attribute files

File names are separated by commas. (That is, do not rewrite struts. properties !)

-->

<Constant name = "struts. Custom. properties"

Value = "application, org/Apache/struts2/extension/custom"/>

<! -- Specify the request URL and action er. The default value is org. Apache. struts2.dispatcher. Mapper. defaultactionmapper. -->

<Constant name = "struts. Mapper. Class" value = "org. Apache. struts2.dispatcher. Mapper. defaultactionmapper"/>

<! -- Specifies the suffix of action. The default value is action. -->

<Constant name = "struts. Action. Extension" value = "do"/>

<! -- Used by filterdispatcher to specify whether the browser caches static content. The test phase is set to false, and the release phase is set to true. -->

<Constant name = "struts. Serve. Static. browsercache" value = "true"/>

<! -- Set whether to support dynamic method calls. True is supported, and false is not supported. -->

<Constant name = "struts. Enable. dynamicmethodinvocation" value = "true"/>

<! -- Set whether a diagonal line can be used in an action. The default value is false. To use it, set it to true. -->

<Constant name = "struts. Enable. slashesinactionnames" value = "true"/>

<! -- Whether expression syntax is allowed. The default value is true. -->

<Constant name = "struts. Tag. altsyntax" value = "true"/>

<! -- Set whether to reload when the Struts. xml file is changed -->

<Constant name = "struts. configuration. xml. Reload" value = "true"/>

<! -- Sets whether struts is in development mode. The default value is false, and the value is set to true in the test phase. -->

<Constant name = "struts. devmode" value = "true"/>

<! -- Set whether to reload the resource file for each request. The default value is false. -->

<Constant name = "struts. i18n. Reload" value = "false"/>

<! -- Standard UI topic. The default UI topic is XHTML, which can be simple, XHTML, or Ajax. -->

<Constant name = "struts. UI. Theme" value = "XHTML"/>

<! -- Template directory -->

<Constant name = "struts. UI. templatedir" value = "template"/>

<! -- Set the template type. It can be FTL, Vm, or orjsp -->

<Constant name = "struts. UI. templatesuffix" value = "FTL"/>

<! -- Locate the velocity. properties file. Default Value: velocity. properties -->

<Constant name = "struts. Velocity. configfile" value = "velocity. properties"/>

<! -- Set the context of velocity. -->

<Constant name = "struts. Velocity. contexts" value = "..."/>

<! -- Locate toolbox -->

<Constant name = "struts. Velocity. toolboxlocation" value = "..."/>

<! -- Specify the web application port -->

<Constant name = "struts. url. http. Port" value = "80"/>

<! -- Specify the encrypted port -->

<Constant name = "struts. url. HTTPS. Port" value = "443"/>

<! -- Set whether to include the parameter when generating the URL. The value can be none or getorall. -->

<Constant name = "struts. url. includeparams" value = "get"/>

<! -- Set the international resource files to be loaded, separated by commas (,). -->

<Constant name = "struts. Custom. i18n. Resources" value = "application"/>

<! -- Some web application servers cannot process httpservletrequest. getparametermap (),

For example, WebLogic, Orion, and andoc4j must be set to true. The default value is false. -->

<Cosntantname = "struts. Dispatcher. parametersworkaround" value = "false"/>

<! -- Specify freemarker manager -->

<Constant name = "struts. freemarker. Manager. classname" value = "org. Apache. struts2.views. freemarker. freemarkermanager"/>

<! -- Set whether to set cache for the freemarker template. The effect is equivalent to copying the template to web_app/templates. -->

<Cosntantname = "struts. freemarker. templatescache" value = "false"/>

<! -- This attribute does not need to be modified. -->

<Constant name = "struts. freemarker. wrapper. altmap" value = "true"/>

<! -- Specify whether to use the style sheet cache for javastresult. Set the development stage to true and the release stage to false. -->

<Cosntantname = "struts. XSLT. nocache" value = "false"/>

<! -- Set the list of files automatically loaded by struts. -->

<Constant name = "struts. configuration. Files" value = "struts-default.xml, struts-plugin.xml, Struts. xml"/>

<! -- Set whether to always select namespace at any location before the last slash. -->

<Constant name = "struts. Mapper. alwaysselectfullnamespace" value = "false"/>

</Struts>

========================================================== ====================

1. In struts2, Why must the action submitted in the form be in the form of XXX. Action? The reason is defined in the resource file (read-only) default. properties. You can modify the configurations in it. (The method is to create the Struts. properties file in the src directory and modify it as needed .), In addition, Why must the configuration file name of struts2 be struts. xml? In fact, these definitions are all configured in the default. properties file (read-only. Of course, you can find it and modify it. (The method is to create the Struts. properties file in the src directory and modify it as needed. The reason is)

2. How to prevent Chinese characters submitted in the form from being garbled after being retrieved: solution:

You only need to add a code between the <struts> </struts> tag of the Struts. xml file.

<Struts>

<Constantname = "struts. i18n. encoding" value = "GBK"> </constant>

</Struts>

Of course, you can also configure transcoding in struts. properties or web. xml. However, the priority is: Web. xml> struts. Properties> struts. xml

Configure the transcoding format in struts. properties (in the src directory:

Struts. i18n. Encoding = GBK

The code for configuring transcoding in Web. XML is: Add the following code in <filter> </filter>:

<Filter>

<Init-param>

<Param-Name> struts. i18n. Encoding </param-Name>

<Param-value> GBK </param-value>

</Init-param>

</Filter>

3. Test the running time of an action (the execution time is output on the console ). You only need struts. add the <package> </package> label in the XML file, such as code (interceptor, details and use need to view the struts-default.xml file ), add a reference to the action tag to be tested.

<Package>

<Interceptors>

<Interceptorname = "timer" class = "com. opensymphony. xwork2.interceptor. timerinterceptor">

</<Interceptorname = "Params" class = "Om. opensymphony. xwork2.interceptor. parametersincerceptor"> // Add this sentence to obtain the parameters of the form.

<Interceptor>

</Interceptor>

</Interceptors>

</Package>

The following is a reference to the above Code:

<Action>

<Interceptor-refname = "timer"/>

<Interceptor-refname = "Params"/> // obtain the form parameters by referencing

</Action>

That is to say, if the timer attribute is added, you must add the Params parameter to obtain the parameters in the form. Similar to the constructor Principle

4. In the Struts. xml file, the <action method = "method name"> </Action> label specifies the method that the action calls the value of the method attribute. The method can be defined in the xxxaction. Java file. For example, the configuration in struts. XML is as follows:

<Actionname = "modify" class = "com. Test. Action. testmethod" method = "modify">

<Resultname = "success">/resultmodify. jsp </result>

</Action>

// Indicates that the modify method exists in the testmethod. Java class. As soon as the browser requests modify. Action, it will automatically go to the corresponding class and call the corresponding mofidy (). Of course, the name attribute name in the <action> </Action> label can be obtained at will. The name of the request must be the same as that of the request.

<Actionname = "query" class = "com. Test. Action. testmethod" method = "query">

<Resultname = "success">/resultquery. jsp </result>

</Action>

// Similar to the above functions

5. Use wildcard ing in the Action tag to match:

* 0-n characters, not including/commonly used

** It must be 0-n characters long, including/

\ Indicates the Escape Character

For example, combine the preceding two action tags into an action tag. As follows:

<Action name = "* User" class = "com. Test. Action. testmethod" method = "{1}">

<Result name = "success">/{1} usersucc. jsp </result>

</Action>

It can represent any acion for user operations. All are processed by the testmethod class. In addition, the value of the method attribute changes with the change of access to call the corresponding methods in the class. The page jumps to the same rule. In this way, the method name must be regular.

Action matching with no wildcard has the highest priority. If there are wildcards, they are all matched in order.

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.