Structs2 in the configuration file-not only to be used but also to understand

Source: Internet
Author: User
Tags constant i18n locale


struct2-configuration file:
The entire configuration can be divided into two chunks, one in the Web.xml file and the other in the STRUTS2 framework. The configuration in the framework also has the configuration of the execution environment and the STRUTS2 component configuration.


Web.xml Configuration
Framework execution Environment configuration (Global configuration options): Struts.properties files
Component configuration file: Struts-default. XML, Struts-plugin.xml, Strtus.xml


1.web.xml configuration:
Filterdispatcher is a filter. Note that in struts2.0.x, the use of the
Org.apache.struts2.dispatcher.FilterDispatcher as the core controller, while the Struts2.1 Changed into a org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter. It is the configuration item for the entire Web application and needs to be configured in Web.xml.

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



If it's a basic Web application, that's all that's left to configure some of the implementation environment configuration (global configuration) of Web applications and the configuration of the components used in Web applications, such as action configuration, interceptor configuration.
The execution environment of Web applications is mainly accomplished through struts.properties. The component configuration in the application is mainly done through Strtus.xml.

2. Struts.properties Documents
This file provides a mechanism for changing the default behavior of the framework. In general, we do not need to change this file if it is not intended to be more convenient for debugging. Where are the default properties stored?
In the project's SRC directory, you can write a name called Struts.properties file, compiled later placed in the/web-inf/classes, the STRUTS2 framework will automatically read the file when it is started, but before reading this file, The name default.properties file is loaded into the Struts2-core-xxx.jar package, and the default configuration is defined in this file, so we can define some configuration in strtus.properties to overwrite the configuration in Default.properties, as If there is no Struts.properties file, the default configuration is used.
Open the Default.properties file we will see the following sections of the class capacity:
Opening the file, you can see some global configuration options, in which we find some of these configurations where Struts.action.extension is configured with struts default request suffix name. For more on the meaning of the configuration, followed by a further explanation, now ignore the meaning of these configurations.
### Used 
by the Defaultactionmapper ### your may provide a comma separated list, e.g. Struts.action.extension=action,jn Lp,do 
### The blank extension allows you to match directory listings as as so pure action names 
### without inte Rfering with static resources. 
Struts.action.extension=action, 
### This can is used to set your default locale and encoding scheme 
# Struts.loca Le=en_us 
Struts.i18n.encoding=utf-8


To change the default settings:
### Used 
by the Defaultactionmapper ### your may provide a comma separated list, e.g. Struts.action.extension=action,jn Lp,do 
### The blank extension allows you to match directory listings as as so pure action names 
### without inte Rfering with static resources. 
Struts.action.extension=action, 
### This can is used to set your default locale and encoding scheme 
# Struts.lo Cale=en_us 
### Used 
by the Defaultactionmapper ### your may provide a comma separated list, e.g. Struts.action.extension=action,jn Lp,do 
### The blank extension allows you to match directory listings as as so pure action names 
### without inte Rfering with static resources. 
Struts.action.extension=action, 
### This can is used to set your default locale and encoding scheme 
# Struts.loca Le=en_us 

Because the default.properties file is stored in a jar package, the struts2 is automatically found when it starts. We can't modify this file directly, but we can use the Struts.properties file to overwrite the contents of the Default.properties file.
You can overwrite the original configuration by creating a new struts.properties in the root of SRC for the WEB project, and then adding the property you want to modify to the file. Note: This file is stored in the root directory of SRC (compiled and placed in the/web-inf/ Classes root directory):
# #激活重新载入国际化文件的功能 
struts.i18n.reload=true 
# #修改请求后缀为action或者do 
struts.action.extension=action,do 
# #打开开发者模式, after opening, we modify the configuration file without restarting the server 
Struts.devmode =true
# #激活重新载入国际化文件的功能 
struts.i18n.reload=true 
# #修改请求后缀为action或者do 
struts.action.extension=action,do 
# #打开开发者模式, after opening, we modify the configuration file without restarting the server 
Struts.devmode =true

3. Struts-default.xml
This file is used to load the component that is started by default. It is stored in the root directory of the Struts2-core-xxx.jar package, and the file is loaded when the system starts. The components configured in this file have type conversion components, interceptor components, result type components, and so on, and the concepts about these components will be covered here, just to understand.
4. Struts-plugin.xml
You can use Plug-ins in Struts2, Struts2 automatically search for the struts-plugin.xml files in the jar package in classpath to load the plug-in at startup. The application of the plugin will be mentioned later.
5. Struts.xml
The Struts.xml file contains the configuration of the action we developed. As in the previous login example configuration:
<?xml version= "1.0" encoding= "UTF-8"?> <! 
DOCTYPE struts public 
"-//apache Software foundation//dtd struts Configuration 2.0//en" 
"http:// Struts.apache.org/dtds/struts-2.0.dtd "> 
<struts> 
<!--configuration constants, overwriting configuration in Default.properties--> 
<constant name= "struts.action.extension" value= "Do"/>
<!--struts2 Action must be placed under the specified packet space--> 
<package name= "Com.wq" extends= "Struts-default" > 
<!--define action--> <action name= 
"Login" class = "Com.wq.web.action.LoginAction" > 
<!--Define the mapping relationship between processing results and resources--> 
<result name= "Success" >/ welcome.jsp</result> 
<result name= "error" >/error.jsp</result> 
</action> 
</package>
</structs>


<?xml version= "1.0" encoding= "UTF-8"?> <! 
DOCTYPE struts public 
"-//apache Software foundation//dtd struts Configuration 2.0//en" 
"http:// Struts.apache.org/dtds/struts-2.0.dtd "> 
<struts> 
<!--configuration constants, overwriting configuration in Default.properties--> 
<constant name= "struts.action.extension" value= "Do"/>
<!--struts2 Action must be placed under the specified packet space--> 
<package name= "Com.wq" extends= "Struts-default" > 
<!--define action--> <action name= 
"Login" class = "Com.wq.web.action.LoginAction" > 
<!--Define the mapping relationship between processing results and resources--> 
<result name= "Success" >/ welcome.jsp</result> 
<result name= "error" >/error.jsp</result> 
</action> 
</package>
</structs>

5.1 Overwrite global configuration in default.properties in Struts.xml
When we modify the global configuration, we use the Struts.properties file to overwrite the contents of the Default.properties file. In fact, we can do not create struts.properties files can also be done to overwrite the default.properties file configuration, is directly in the Strtuts.xml file using <constant> configuration: See the example above

5.2 Splitting the struts.xml into multiple configuration files
As you can imagine, there will always be an action need to be defined in the Web application, so that the Struts.xml configuration will be more and more, the file will be larger and bigger.
To avoid struts.xml files that are too large, bloated, and to improve the readability of strtus.xml files, we can decompose a struts.xml file into multiple configuration files and then include other profiles in Strtus.xml files. So struts2 can use the modular way to manage s Truts.xml the configuration file.
We can use <include> in the configuration to include another configuration file. Let's create a new Struts-hello.xml file, and then include the file in the Struts.xml file

In the configuration file in Structs2
Structs Tags: <include file= "Struts-hello.xml"/> to introduce Structs-hello.xml file to achieve modular management.


6. configuration file Loading order
The configuration file is loaded sequentially from top to bottom: we can open the source code for struts and set breakpoints to track the boot order. By analyzing the source code, the following conclusions are obtained:
1.default.properties:
This file is stored in the ORG.APACHE.STRUTS2 package in Strtus2-core-xxx.jar, the default global configuration
2.struts-default.xml
The file is stored in the root directory in Strtus2-core-xxx.jar and loads the default components, including a series of interceptors and converters
3.struts-plugin.xml
If a plug-in is configured for the application, the file will be present in the plug-in's jar file and it will be automatically loaded
4.struts.xml
The configuration file that you create is not renamed to store custom components such as action or interceptors. The default configuration in the Default.properties file can also be overwritten in this file
5.struts.properties
The configuration file that you created, cannot be renamed to modify the global configuration, In general, the global configuration we are about to modify is placed in the Struts.xml file, so there is no need to configure this file. If you are configuring both in Struts.xml and Struts.properteis, whichever is the strtus.properties

7. struts.xml-Package Configuration:
In Strtuts2, the core components are action, interceptors, etc., and the STRUTS2 framework uses packages to manage action and interceptors. Each package is a collection of multiple action, multiple interceptors, and so on. Package has the following attributes:
name: This is a required property that specifies the name of the package, which is the key that the package is referenced by another package
extends: Optional attribute. Specifies that the package inherits other packages. Inheriting other packages, you can inherit the action definitions from other packages.
abstract: Optional attribute. Specifies whether the package is an abstract package. The abstract package cannot contain an action definition.

In the previous configuration: inherited the Stuts2 default package Struts-default, where is the default package defined? We can see that there is a struts-default.xml file in the Struts2-core-xxx.jar package

This file is configured with a lot of <bean> tags and a <package> tag,<package> The name of the label is Struts-default. This default packet space defines the STRUTS2 result type, interceptor, and so on. The Struts2 frame will automatically load the file each time. We inherited the default package space in the Strtus.xml file, so the Struts-default.xml file must be loaded before the Strtus.xml file.
Only the correct parent package is inherited to use the required preconfigured features. In most cases, we should all inherit the "Strust-default" in the "struts-default.xml" configuration file Package

8.struts.xml-action configuration:
The action is just a controller, and it does not generate any response directly to the requester. Therefore, when the action finishes processing a user request, the action needs to present the specified view resource to the user. Therefore, when you configure the action, you should configure the mapping between the logical view and the physical view resource.
The mapping relationship between the configuration logical view and the physical view is defined by <result>, and each <result> element defines a mapping between the logical and physical views, such as the following configuration:
<struts> 
<constant name= "struts.action.extension" value= "Do" ></constant> 
<package Name= "Com.wq" extends= "Struts-default" > 
<action name= "Login" class= "Com.wq.web.action.LoginAction" > 
<result name= "Success" >/welcome.jsp</result> 
<result name= "error" >/error.jsp</ result> 
</action> 
</package> 
</struts>


<struts> 
<constant name= "struts.action.extension" value= "Do" ></constant> 
<package Name= "Com.wq" extends= "Struts-default" > 
<action name= "Login" class= "Com.wq.web.action.LoginAction" > 
<result name= "Success" >/welcome.jsp</result> 
<result name= "error" >/error.jsp</ result> 
</action> 
</package> 
</struts>

The following is a description of the properties in the action configuration:
Name: Provides the URL address for the execution action, which is "login.do" (we have changed the suffix of the request path to *.do), and the default is Login.action.
The complete class name of the Class:action class

Let's look at the <result> tags below:
We can see that there are more "name" attributes in the result node, in fact this property is always there, if the developer does not explicitly specify its value, then its default value is "Success", so the above configuration can be changed to:
<action name= "Login" class= "com.wq.web.action.LoginAction" > 
<result >/welcome.jsp</result> 
<result name= "error" >/error.jsp</result> 
</action> 
</package> 
</ Struts>


<action name= "Login" class= "com.wq.web.action.LoginAction" > 
<result >/welcome.jsp</result> 
<result name= "error" >/error.jsp</result> 
</action> 
</package> 
</ Struts>

Match action with wildcard characters:
First look at a domo:
<?xml version= "1.0"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1.7//en" "Http://struts.apache.or G/dtds/struts-2.1.7.dtd "> <struts> <constant name=" s "value=" "></constant> <!--Set Development mode > <constant name= "Struts.devmode" value= "true"/> <constant name= "struts.action.extension" value= "ac Tion,do "/> <!--these three lines add--> <constant name=" Struts.ui.theme "value=" simple "/> To solve the problem of not wrapping. Nstant name= "Struts.ui.templateDir" value= "template"/> <constant name= "Struts.ui.templateSuffix" value= "FTL"/ > <package name= "cn.ouuo.action" extends= "Struts-default" > <action name= "Searchus"
            Er_* "class=" searchaction "method=" {1} "> <result name=" Listresult ">/listResult.jsp</result> <result name= "Listdata" >/more.jsp</result> <result name= "Wordpre">/officePre.jsp</result> <result name= "manager" >/manager.jsp</result> <re  Sult name= "Success" >/success.jsp</result> </action> </package> </struts>


<?xml version= "1.0"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1.7//en" "Http://struts.apache.or G/dtds/struts-2.1.7.dtd "> <struts> <constant name=" s "value=" "></constant> <!--Set Development mode > <constant name= "Struts.devmode" value= "true"/> <constant name= "struts.action.extension" value= "ac Tion,do "/> <!--these three lines add--> <constant name=" Struts.ui.theme "value=" simple "/> To solve the problem of not wrapping. Nstant name= "Struts.ui.templateDir" value= "template"/> <constant name= "Struts.ui.templateSuffix" value= "FTL"/ > <package name= "cn.ouuo.action" extends= "Struts-default" > <action name= "Searchus"
            Er_* "class=" searchaction "method=" {1} "> <result name=" Listresult ">/listResult.jsp</result> <result name= "Listdata" >/more.jsp</result> <result name= "Wordpre">/officePre.jsp</result> <result name= "manager" >/manager.jsp</result> <re  Sult name= "Success" >/success.jsp</result> </action> </package> </struts>

Here I would like to say a bit about their previous confusion, mainly in the action configuration of several parameters, in fact, the Name property is to access the URL address, Method 1 is in the name of the * to match, the way you define the action in each method, The name attribute in result is the string that you return in action, depending on the property to determine the jump page

For example, to visit: http:localhost:8080/ouuo/searchuser_manager.action

Reprint Address: http://spring-g.iteye.com/blog/1286153

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.