struts2: detailed explanation of struts.xml configuration file

struts2: Detailed explanation of struts.xml configuration file
1. Several important elements
1.1 package element The

package element is used to configure the package. In the Struts2 framework, a package is an independent unit, and the package is uniquely identified by the name attribute. You can also let a package inherit another package through the extends property. The value of the extends property is the value of the name property of the inherited package. The inherited package can inherit from the inherited package to interceptors, actions, etc.

In the Struts2 framework, configuration information such as action, result, interceptor, and interceptor-stack is managed through packages. The package attributes are as follows:

Attribute


Description
name Package name, which is used as the mark of other packages to apply this package
extends Optional attribute, set this package to inherit other package
namespace Optional attribute, set the namespace of the package
abstract Optional attribute, set to abstract package-

extends

    When a package inherits another package by configuring the extends property, the package will inherit all the configurations in the parent package, including action, result, interceptor, etc.
    Since the acquisition of package information is carried out in the order of configuration files, the parent package must be defined before the child package.
    Usually when we configure struts.xml, we inherit a package named "struts-default.xml", which is a built-in package in struts2.

-namespace

    This property can specify the namespace corresponding to the package. Since Actions with the same name may coexist in a Web application, in order to avoid naming conflicts, it is enough to make the Actions with the same name in different namespaces.
    In Struts2, if no namespace is specified for a package, the package uses the default namespace, which is always "".
    When the namespace is set to "/", that is, when the namespace of the package is specified as the root namespace, all Action requests under the root path will go to this package to find the corresponding resource information.
    The search for Action in the root namespace is the same as that in the ordinary namespace, that is, if there is a request for "/stu_info.action" (or "/C/stu_info.action"), the Action (or "/C/stu_info.action") in the root namespace is searched first is the Action in the /C namespace), if there is no corresponding Action, look for the Action in the default namespace. This rule is also true for multi-level namespaces, that is, if a request is made to find /A/A_Login/login.action, the Struts2 framework first searches for the action named login in the namespace of /A/A_Login. If it is not found, then Query in the default namespace, not in its upper level "/A".

Demonstration:

    <package name="caiwu" extends="struts-default" namespace="/caiwu">
    </package>

1.

2 action element The Struts2 framework handles HTTP requests through the Action object, and the Action corresponding to the URL address of the request is configured in the action element.

action element attribute

attribute name


function description
name The name of the requested Action
class Optional attribute, the Action processing class corresponds to the specific path
method Optional attribute, specify the method name in the Action
converter Optional attribute, specify the type converter used by the Action

    If no method is specified, the default execution in the Action class execute method; otherwise calls the method specified in the method attribute.
    If no class is specified, the default value is: com.opensymphony.xwork2.ActionSupport, which uses the default processing method (does nothing) and returns the success value directly.

Demonstration:

        <action name="list" class="com.clzhang.struts2.demo3.ListAction">
        </action>
        <action name="listSalarySum" class="com.clzhang.struts2.demo3.ListAction" method=" listSalarySum">
        </action>

1.

3 result element When the Action method is called and the processing returns, the next step is to use the result element to set the view returned to the browser. When configuring the result element, you often need to specify the name and type attributes.

result property

Property name


Function description
name The name of the logical view returned by the corresponding Action, the default is success
type Returns the result type. The default is that the dispatcher

    name attribute corresponds to the value returned by the Action method, and success is its default value.
    The type attribute specifies the result type, the default type is dispatcher.

Result types supported by Struts2

Result  type
Description
dispatcher Forward the request to the specified JSP page
redirect Redirect the request to the specified view resource
chain Process the Action chain
freemarker Specify the Freemarker template as the view
httpheader Control special HTTP behavior
redirect-action Direct Jump to other Action
stream Return an InputStream to the browser (usually used for file download)                   
velocity Specify the velocity template as the view
xslt Use for XM/XSLT integration plainText
Display the original code of a page Example : Copy code         <action name=" login" class="com.clzhang.struts2.demo1.LoginAction">





            <result name="input">/struts2/demo1/login.jsp</result>
            <result name="error">/struts2/demo1/wrong.jsp</result>
            <result name="list" type=" redirectAction">
                <param name="idInList">${id}</param>
                <param name="actionName">listBook</param>
            </result>
        </action>

Copy code

Need to pay attention to the difference between dispatcher and redirect, That is, the difference between forwarding and redirection, redirection will lose all request parameters, and will lose the Action processing result.
1.4 include element A default struts.xml file is provided

in Struts2, but if there are many configurations such as package, action, interceptors, etc., it is not easy to maintain in a struts.xml file. Therefore, it is necessary to divide the struts.xml file into multiple configuration files, and then use <include> in the struts.xml file Tags refer to these configuration files.

Demonstration:

    <include file="caiwu.xml"></include>
    <include file="cangku.xml"></include>

1.5 global-results element

There are many times when a <result> is used at the beginning of a lot of <action>, then you can use the <global-results> tag to define the global <result> .

Demonstration:

        <global-results>
            <result name="user">/struts2/demo3/user.jsp</result>
            <result name="sum">/struts2/demo3/sum.jsp</result>
            <result name ="default">/struts2/demo3/default.jsp</result>
        </global-results>

1.6 default-action-ref element

If an Action resource is not defined, the system will throw a 404 error. Such mistakes are inevitable, but such pages are not friendly. We can use <default-action-ref> to specify a default Action. If the system does not find the specified Action, it will specify to call the default Action.

Demonstration:

    <

        <result>/jsp/actionError.jsp</result>
    </action>

2. Dynamic invocation of Action (DMI)

Struts2 provides an Action processing method that includes multiple processing logics, namely DMI (Dynamic Method Invocation, dynamic method invocation) . It implements dynamic operations through a specific method in the request object. Specifically, add the request method string after the URL address of the requested Action to match the method in the Action object. Among them, the Action object name and the method are separated by "!".

For more content, refer to: struts2: Multi-service method processing (dynamic invocation, DMI)

demonstration:

<A href="list!listUser.action" target="_blank">3.2 Through URL exclamation mark parameters</A>

3. Wildcards

are in In actual project development, most of the definitions of multiple Actions will be the same, which will result in a lot of redundancy. For this situation, Struts2 also gives a corresponding solution, that is, using wildcards.
Wildcard

Description
* matches 0 or more characters except "/"
** matches 0 or more characters including "/"
\character escape character, "\\" matches "\"; "\*" matches "*"                                                        

The wildcard "*" is usually used in the name attribute of the <action> tag, and in the class, name attribute and result element, the form of {n} is used to represent the string matched by the nth *, and {0} is used to represent The entire Action string for the URL request.

Demonstration 1:

<!-- Use * wildcards, the first * means calling method, the second * means Action -->
<action name="*_*" class="com.clzhang.struts2.action.{2 }Action"
    method="{1}">
    <result name="success">/{0}Suc.jsp</result>
</action>

In the above code, when the URL request is /update_Login.action, it will Call the update() method in the LoginAction class, and return to update_LoginSuc.jsp at the end of the process.

Demonstration 2:

<!-- No matter which Action is called, the JSP named Action name is returned by default -->
<action name="*_*">
    <result>/{0}.jsp</result>
</action>

The above code does not specify the class attribute, nor the name of the result element, so no matter which Action is accessed, the JSP page with the same name as the Action will be returned.
4.

In the introduction to the struts.properties configuration file, we mentioned that all the properties defined in the struts.properties file can be configured in the struts.xml file. In struts.xml, it is configured through the <constant> tag.

Demonstration:

    <constant name="struts.action.extension" value="action"></constant>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
    <constant name="struts.ui. theme" value="simple"></constant>
    <constant name="struts.custom.i18n.resources" value="message"></constant>
    <constant name="struts.i18n.encoding" value="UTF -8"></constant>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326797754&siteId=291194637