Struct.xml Action Configuration

Source: Internet
Author: User

< Packagename= "Default"namespace="/"extends= "Struts-default">                <Actionname= "Hello">           <result>/hello.jsp</result>        </Action>    </ Package>

The process by which a struct looks for a destination view through this action is actually divided into the following steps:

1. Find the action that matches the address in the URL name

2. after finding this action, look at his class and method attributes , and get the return value (must be a string) of the method that corresponds to this class object

class Default value is Actionsupport

Method default value is Execute

3. Search for result with name matching the return value

So the action is configured with the following three types:

The return of a specific view can be determined by the user's own defined action

The specific method is to find the corresponding configuration item based on the returned string to determine the content of the view

Handling of the class of Action

    • The implementation of the specific action can be a common Java class with the public String Execute method (Forget)
    • Implement the action interface and implement his Execute method (forget)
    • Inheriting from the Actionsupport class, the advantage is that it can be used directly with the Struct2 encapsulated method.

If the action does not have a class attribute, then the default is Actionsupport.

See actionsupport This class of source code, will find a method:

 Public throws Exception {        return  SUCCESS;    }

In fact, the struct is the default invocation of this method

However, the Execute method does not necessarily execute when the action is executed

Action Configuration

    • 1 can be configured by adding a method property when configuring the action
<name= "UserAdd"  class= "Com.bjsxt.struts2.user.action.UserAction "  method=" Add ">     <result>/user_add_ success.jsp</result></action>

Forget, too inflexible, will generate a lot of action (one action on a method)

    • 2 dynamically specified in URL address (dynamic method call DMI)

< Packagename= "User"extends= "Struts-default"namespace= "/user">                   <Actionname= "User"class= "Com.bjsxt.struts2.user.action.UserAction">            <result>/user_add_success.jsp</result>        </Action>    </ Package>

Then you need to match the URL, when configuring the URL, use an exclamation point to separate the method

<href= "<%=context%>/user/user!add"> Add user </  A><href= "<%=context%>/user/user!delete"  > Delete user </a>

In this case, only the above action is required for the same class, but note that there are different ways to write in useraction

    • 3. Wildcard characters

<Struts>    <constantname= "Struts.devmode"value= "true" />    < Packagename= "Actions"extends= "Struts-default"namespace= "/actions">        <Actionname= "student*"class= "Com.bjsxt.struts2.action.StudentAction"Method= "{1}">            <result>/student{1}_success.jsp</result>        </Action>                <Actionname="*_*"class= "Com.bjsxt.struts2.action." {1} Action "Method= "{2}">            <result>/{1}_{2}_success.jsp</result>            <!--{0}_success.jsp -        </Action>    </ Package></Struts>

{1} represents the first *

with wildcard characters, you can minimize the amount of configuration

Adherence to the convention is greater than the principle of configuration

Match order

    • If more than one action is met, the level without wildcards is high
    • As long as it is a level that contains wildcards, who matches who in front of

Struct.xml Action Configuration

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.