Package,action,result,method configuration in "Turn-organize" Struts2

Source: Internet
Author: User

1 <?XML version= "1.0" encoding= "UTF-8"?>  2 <!DOCTYPE Struts public3 "-//apache software foundation//dtd Struts Configuration 2.3//en"4 "Http://struts.apache.org/dtds/struts-2.3.dtd">  5 <Struts>  6     <!--/primer/helloworldaction.action7 Package: Packages8 * Name: Package name, unique, required option9 * Namespace: namespace, unique, equivalent to room number. Optional, omit case is "/". The first half of the request connection in the pageTen * Extends: Inheritance One * extends= "struts-default": Struts-default.xml file under Core package Struts2-core-2.3.3.jar provided by STRUTS2 framework A * Why inherit this struts-default.xml file?  - * Because the struts-default.xml provided at the bottom of the STRUTS2 framework declares all the interceptors and interceptor stacks, - know that the interceptor stack in the Struts-default.xml file is executed while the STRUTS2 framework is running.  the * If you do not inherit the Struts-default.xml file, there is no way to use all interceptors provided by the STRUTS2 framework -       -   -     < Packagename= "Primer"namespace= "/primer"extends= "Struts-default">   -            +         <!--    - if the corresponding action name cannot be found, configure the action to be executed by default + * Name: Specify the name of the action A          -   at         <Default-action-refname= "Helloworldaction" />   -            -         <!--    - Action: - * Name: The back part of the request connection in the corresponding page - * Class: Corresponds to the full path of the class to be executed in           -   -         <Actionname= "Helloworldaction"class= "Cn.yht.primer.HelloWorldAction">   to             <!--    + Result: Results type - * Name: Corresponds to the return value of the method that executes the class the Public String Execute () throws Exception { * System.out.println ("Helloworldaction ************* Execute ()");  $ return "Success"; Panax Notoginseng                         }   - * Text content in the second half: the page to turn to the               -   +             <resultname= "Success">/primer/success.jsp</result>   A         </Action>   the         <!--    + no class is specified for action - * The default execution class is configured in the Struts-default.xml file at the bottom of the STRUTS2 framework $ Com.opensymphony.xwork2.ActionSupport $ Public String Execute () throws Exception { - return SUCCESS;  -                         }   the * In effect, the Execute () method of the Actionsupport class provided by the underlying is performed by default - * result type, default is the return value of the Execute () method of the Actionsupport class provided by the underlying STRUTS2 framework, to jumpWuyi           -   the         <Actionname= "Actionnoclass">   -             <resultname= "Success">/primer/success.jsp</result>   Wu         </Action>   -     </ Package>   About </Struts>  

To configure the method property for an action:

Each processing method in the action class is defined as a logical action method.

1 <!DOCTYPE Struts public2 3 "-//apache software foundation//dtd Struts Configuration 2.0//en"4 5 "Http://struts.apache.org/dtds/struts-2.0.dtd">6 7 <Struts>8 9     < Packagename= "my"extends= "Struts-default"namespace= "/manage">Ten  One         <Actionname= "Userlogin"class= "Org.qiujy.web.struts2.action.LoginAction"Method= "Login"> A  -             <resultname= "Success">/success.jsp</result> -  the             <resultname= "Error">/error.jsp</result> -  -         </Action> -  +          -  +         <Actionname= "Userregist"class= "Org.qiujy.web.struts2.action.LoginAction"Method= "Regist"> A  at             <resultname= "Success">/success.jsp</result> -  -             <resultname= "Error">/error.jsp</result> -  -         </Action> -  in     </ Package> -  to </Struts>

As above, the login and Regist methods in Loginaction are configured as logical action. To invoke the login method, set the action of the form element in index.jsp to "manage/userlogin.action"; to invoke the Regist method, set the action of the form element in regist.jsp to " Manage/userregist.action ".

3.3. Use the wildcard mapping (wildcard mappings) mode:

When you configure the <action...> element in a struts.xml file, its name, class, and Method properties support wildcards, and this wildcard is another form of dynamic method invocation.

When we use wildcards to define the Name property of an action, it is equivalent to defining more than one logical action with an action of an element:

<Actionname= "User_*"class= "Org.qiujy.web.struts2.action.UserAction"Method= "{1}">            <resultname= "Success">/success.jsp</result>            <resultname= "Error">/error.jsp</result>        </Action>{1} is a configuration parameter, meaning: user_*, when executed, when the method *,user_a is used, then the method A is used. 

As above, <action name= "user_*" > defines a series of request URLs that are logical actions of the user_*.action pattern. The value of the method property is an expression {1}, indicating that its value is the value of the first * in the Name property value. For example, when a user requests a URL of user_login.action, the login method to the Useraction class is called, and the Regist method to the Useraction class is called when the user requests a URL of user_regist.action.

4. Processing results

When the STRUTS2 action finishes processing the user request, a normal string is returned, and the entire normal string is a logical view name. Struts2 by configuring the mapping between the logical view name and the physical view resource, once the system receives the name of a logical view returned by the action, the system renders the corresponding physical view resource to the browser.

4.1. Configuration Processing results:

Struts2 's action handles the end of the user request, returns a normal string-logical view name, which must be mapped in the Struts.xml file for logical and physical view resources before the system can go to the actual view resource.

Struts2 configures the results by using the <result .../> element in the Struts.xml file. The STRUTS2 provides two results.

L Local Result: The <result .../> is configured as a child element of the <action ...> element.

L Global Result: <result .../> is configured as a child element of the <global-results ...> element.

Configure <global-results> child elements in the package element:

<Global-results><resultname= "Error">/error.jsp</result><resultname= "Invalid.token">/error.jsp</result><resultname= "Login"type= "Redirect-action">Logon!input</result></Global-results>
Global result (Global-results)
There are many times a <result> is available for many <action> use, you can use <global-results> tags to define global <result>l. Execution order: When a string returned by an action does not correspond to the corresponding <result>, STRUTS2 will find the global <result>.

Package,action,result,method configuration in "Turn-organize" Struts2

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.