Struts.xml configuration (all basic configuration, including wildcard characters)

Source: Internet
Author: User

Struts.xml is the most efficient file in development and the most important profile in Struts2. Here are some of the tags that are commonly used in several struts.xml.

1, <include>

With the include tag, you can split a struts.xml configuration file into multiple profiles, and then use <include> tags in struts.xml to introduce additional configuration files. For example, an online shopping program, you can put user configuration, product configuration, order configuration in 3 profiles User.xml, Goods.xml and Order.xml, and then in Struts.xml to introduce these 3 profiles:

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>

In the previous introduction to the Struts.properties configuration file, we mentioned that all the attributes defined in the Struts.properties file can be configured in the Struts.xml file. In Struts.xml, it is configured through 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>
    <!--set Development mode-->
    <constant name=" Struts.devmode "value= true"/>
    <!--set encoded as gb2312--> <constant name=
    "Struts.i18n.encoding" Value= "GB2312"/>
    <!--omit additional configuration information-->
</struts>

3, <package> 1. Introduction to package Properties

In the STRUTS2 framework is the package to manage action, result, interceptor, interceptor-stack configuration information. Package properties are as follows:

Property

is required

Describe

Name

Is

The package name, which is used as the tag for the other package

Extends

Whether

Set this package to inherit other packages

Namespace

Whether

To set the namespace of a package

Abstact

Whether

Set as abstract package

2, extends property of the details when a package inherits another package by configuring the extends property, the package inherits all of the configuration from the parent package, including action, result, interceptor, and so on. Because package information is obtained in 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 built-in package in struts2.

3, namespace of the detailed

namespace is mainly for the management of action in large projects, and more important is to solve the problem of the name of the action, because the action 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 for the action under this configuration is http://localhost:8080/login.action if the 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 for 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, when the namespace of the package is specified as the root namespace, the action request in all root paths will then look for the corresponding resource information in the package.

If the path in the previous precedent is http://localhost:8080/login.action, 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

Action-handling classes correspond to specific paths

Method

Whether

Specify a method name in action

Converter

Whether

Specifies the type converter to use for the action

The Execute method in the action is executed by default if no methods are specified. 2, <result> attribute introduction

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, default to Dispatcher

3, the use of wildcard characters

As result increases, struts.xml files become more complex. Then you can use wildcards to simplify the configuration:

For example, the following case:

Action for Test.java

public class Test {public
    String test1 () {return
        "RESULT1";
    }
     
    Public String test2 () {return
        "result2";
    }
     
    Public String Test3 () {return
        ' RESULT3 ';
    }
}

Struts.xml is configured in the following:

<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, access to the action method of another way to implement

In Struts2, if you want to access the specified method in the action, you can also change the URL request to make the original action name. Action the action name. Method name. Action "You do not need to specify a method name in Struts.xml.

5, <exception-mapping> and <global-exception-mapping>

Both of these tags are used to configure the view information corresponding to the occurrence of the exception. Only one is a range of action, one is a package scope, and when the same type of exception is configured in two scopes, the action scope priority is higher than the package scope priority. The two tags contain the same properties:

Property name

Whether you must

Function description

Name

Whether

Used to represent this 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 an exception type

The sample code for the two tabs 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, if no specific class value is specified for an action, the system will automatically reference the class specified in the <default-class-ref> label. In the STRUTS2 framework, the default class for the system is actionsupport, which we can find in the Xwork-default.xml file under Xwork's 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 executing ...");
    }


Configure 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 ask for an action resource that is not defined, the system throws a 404 error. This error is unavoidable, but such a page is 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>

The label is used to set the default interceptor information to be applied to all the action in the entire package scope. In fact, our package inherits the Struts-default package and uses the default settings for 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 requirements can change the default interceptor configuration. Once this configuration is changed, "Defaultstack" will no longer be referenced and needs to be manually added.

9, <interceptors>

This tab allows you to register interceptors or interceptor stacks into the STRUTS2 framework, typically for custom interceptors or interceptor stacks. The label uses the following method:

<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 to the action where it resides. The interceptor specified in,<default-interceptor-ref> will no longer work on this action when the Interceptor feature is added separately for an action.

11, <global-results>

The label is used to set the global result set within the package scope. When multiple action returns the same logical view, the logical view of these physical views can be uniformly configured through the <global-results> 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>
    <package name=" Wwfy "extends=" Struts-default ">
        <global-results>
            <result name=" test ">/index.jsp</result>
        < /global-results>
    </package>
</struts>


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.