Struts.xml configuration detailed 1, <include>
With the include tag, you can split a struts.xml profile into multiple profiles, and then use the <include> tags in the struts.xml to introduce additional configuration files.
For example, an online shopping program, you can put the user configuration, product configuration, order configuration in 3 configuration files User.xml, Goods.xml and Order.xml, and then in Struts.xml will be introduced in the 3 configuration files:
Struts.xml:
<?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> <include file=" User.xml "/> <include file= "Goods.xml"/> <include file= "Order.xml"/></struts>
User.xml:
<?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> <package name=" Wwfy "extends=" Struts-default " > <action name= "Login" class= "wwfy.user.LoginAction" > <!--omit action other configuration-- </action > <action name= "logout" class= "wwfy.user.LogoutAction" > <!--omit action other configuration-- </ Action> </package></struts>
2. <constant>
All properties defined in the Struts.properties file can be configured in the Struts.xml file. In Struts.xml, it is configured with the <constant> tag:
<?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> <!--setting Development mode-- <constant name=" Struts.devmode "value=" true "/> <!--settings encoded as gb2312--> <constant name=" Struts.i18n.encoding " Value= "GB2312"/> <!--omit additional configuration information--></struts>
3, <package>1, package property Introduction
In the STRUTS2 framework, you manage configuration information such as action, result, interceptor, interceptor-stack, and so on through a package. The package properties are as follows:
Property |
is required |
Describe |
Name |
Is |
Package name, which is used as a tag for other packages to apply this package |
Extends |
Whether |
Set this package to inherit other packages |
Namespace |
Whether |
Set the namespace of a package |
Abstact |
Whether |
Set as abstract package |
2, extends property of the detailed
- When a package inherits another package by configuring the extends property, the package inherits all the configuration from the parent package, including action, result, interceptor, and so on.
- Because the package information is obtained according to the order of the configuration file, the parent package must be defined before the child package.
- Usually when we configure Struts.xml, we inherit a package called "Struts-default.xml", which is a package built into struts2.
3, namespace of the detailed
Namespace is primarily for the management of actions in large projects, and more importantly, to address the issue of action names, because actions that are not in the same namespace can use the same action name.
1) If the namespace is used, the URL will change
For example, we have a configuration file
<package name= "Wwfy" extends= "Struts-default" > <action name= "Login" class= "Wwfy.action.LoginAction" > <result>/success.jsp</result> </action></package>
The URL of the action under this configuration is Http://localhost:8080/login.action
If a namespace is specified for this package
<package name= "Wwfy" extends= "Struts-default" namespace= "/user" > <action name= "Login" class= " Wwfy.action.LoginAction "> <result>/success.jsp</result> </action></package>
The URL of the action under this configuration is Http://localhost:8080/user/login.action
2) Default namespace
Struts2 If you do not specify a namespace for a package, the package uses the default namespace, and the default namespace is always "".
3) Specify the root namespace
When the namespace is set to "/", that is, the namespace of the package specified is the root namespace, the action request under all root paths will go to the package to find the corresponding resource information.
If the path is http://localhost:8080/login.action in the previous precedent, all http://localhost:8080/*.action will look for resources in the package set to the root namespace.
4, <action> and <result>1, <action> attribute Introduction
Property name |
Whether you must |
Function description |
Name |
Is |
The requested action name |
Class |
Whether |
The action handler class corresponds to the specific path |
Method |
Whether |
Specify the name of the method in the action |
Converter |
Whether |
Specifies the type converter used by the action |
If no method is specified, the Execute method in action is executed by default.
2. Introduction to <result> Properties
Property name |
Whether you must |
Function description |
Name |
Whether |
The corresponding action returns the logical view name, which defaults to success |
Type |
Whether |
Returns the result type, which defaults to dispatcher |
3. Use of wildcard characters
As result increases, the Struts.xml file becomes more complex. Then you can use wildcards to simplify configuration:
For example, the following case:
Action is Test.java
public class Test {public String test1 () { return "RESULT1"; } Public String test2 () { return "result2"; } Public String test3 () { return ' RESULT3 '; }}
Configured as Struts.xml in the
<package name= "Wwfy" extends= "Struts-default" > <action name= "test*" class= "Wwfy.action.test{1}" > <result name= "Result{1}" >/result{1}.jsp</result> </action></package>
4. Another way to access the action method
In Struts2, if you want to access the specified method in the action, you can also change the URL request by changing the original "action name. Action" to "Action name!" Method name. Action "You don't need to specify a method name in Struts.xml.
5, <exception-mapping> and <global-exception-mapping>
Both tags are used to configure the view information corresponding to the exception, except that one is an action range, one is a package scope, and when the same type of exception is configured in two scopes, the action scope takes precedence over the packet scope. The two tags contain the same attributes:
Property name |
Whether you must |
Function description |
Name |
Whether |
Used to represent the exception configuration information |
Result |
Is |
Specifies the view information to display when an exception occurs, which is configured as a logical view |
exception |
Is |
Specifying the exception type |
The sample code for the two tags is:
<?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> <package name=" Default "extends=" Struts-default " > <global-exception-mappings> <exception-mapping result= "Logical View" exception= "Exception type"/> </global-exception-mappings> <action name= "action name" > <exception-mapping result= "Logical View" exception= "Exception type"/> </action> </package></struts>
6. <default-class-ref>
When we configure the action, the system automatically references the class specified in the <default-class-ref> tag if no specific class value is specified for an action. In the STRUTS2 framework, the system default class is Actionsupport, which we can find in the Xwork-default.xml file under the Xwork core package.
When you have special needs, you can manually specify the default class
Package wwfy.action; public class Defaultclassref {public void execute () { System.out.println ("Default class begins execution ...");} }
Configuring in Struts.xml
<?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> <package name=" Wwfy "extends=" Struts-default " > <!--specifies that the default class is test- <default-class-ref class= "Wwfy.action.DefaultClassRef"/> <action name= "Test1" > <result>/index.jsp</result> </action> </package ></struts>
7. <default-action-ref>
If you request an action resource that is not defined, the system throws a 404 error. Such errors are unavoidable, but such pages are not friendly. We can use <default-action-ref> to specify a default action, and if the system does not find the specified action, it is specified to invoke the default action.
<?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> <package name=" Wwfy "extends=" Struts-default " > <default-action-ref name= "Acctionerror" ></default-action-ref> <action name= " Acctionerror "> <result>/jsp/actionError.jsp</result> </action> </package ></struts>
8. <default-interceptor-ref>
This tab is used to set the default interceptor information to be applied to all actions within the entire package scope. In fact, our package inherits the Struts-default package, using the default settings of struts. We can find the relevant configuration in Struts-default.xml:
<default-interceptor-ref name= "Defaultstack"/>
In the actual development process, if we have special needs, we can change the default interceptor configuration. Once this configuration was changed, "Defaultstack" would no longer be referenced and needed to be manually added.
9. <interceptors>
This tag allows you to register an interceptor or interceptor stack with the STRUTS2 framework, which is typically used for registering a custom interceptor or interceptor stack. Here's how to use the label:
<interceptors> <interceptor name= "Interceptor name" class= "Interceptor class"/> <interceptor-stack name= "Interceptor stack name" > <interceptor-ref name= "Interceptor name" > </interceptor-stack></interceptors>
10. <interceptor-ref>
This tab allows you to add interceptor functionality for the action it is in. When the Interceptor function is added separately for an action, the interceptor specified in,<default-interceptor-ref> will no longer function with this action.
11. <global-results>
The label is used to set the global result set within the package scope. With multiple actions returning the same logical view, the logical view corresponding to these physical views can be configured uniformly through the <global-results> label.
<?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> <package name=" Wwfy "extends=" Struts-default " > <global-results> <result name= "test" >/index.jsp</result> </global-results > </package></struts>
002--struts.xml Configuration Detailed