Struts2 (3), struts2 (advanced tutorial)

Source: Internet
Author: User

Struts2 (3), struts2 (advanced tutorial)
I. usage of the namespace attribute of <package> In struts. xml

In actual development, <action> with the same name is often encountered, as shown in the following code:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="default" namespace="/" extends="struts-default"><action name="add" class="action.UserAction" method="add"><result >addUser.jsp</result></action><action name="add" class="action.HouseAction" method="add"><result>addHouse.jsp</result></action>    </package></struts>

In the above configuration, there are two <action> with the same name as add in the same namespace, so there is no difference. To solve this problem, we can place two <actions> in different namespaces. The Code is as follows:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="user" namespace="/user" extends="struts-default"><action name="add" class="action.UserAction" method="add"><result >addUser.jsp</result></action>    </package>
    <package name="house" namespace="/house" extends="struts-default"><action name="add" class="action.HouseAction" method="add"><result>addHouse.jsp</result></action>    </package>
</struts>

In this way, different modules can be differentiated under different namespaces. However, when requesting an action, add the corresponding namespace name before the action name in the URL. For example, the first request uses http: localhost: 8080/Struts2Demo/user/add

Ii. Use the <include> struts. xml configuration file containing the split

In large projects, a struts. xml configuration is too large and difficult to maintain. You need to split struts. xml.

The split struts. xml file:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <include file="struts-house.xml"/>     <include file="struts-add.xml"/></struts>

Struts-house.xml file after split:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="house" namespace="/house" extends="struts-default"><action name="add" class="action.HouseAction" method="add"><result>addHouse.jsp</result></action>    </package></struts>

Struts-user.xml file after split:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="user" namespace="/user" extends="struts-default"><action name="add" class="action.UserAction" method="add"><result >addUser.jsp</result></action>    </package></struts>
Iii. configuration of <result> 1. Use the name attribute in the <result> label

The action class inherited Action interface defines static constant strings returned by several action methods.

1. SUCCESS: indicates that the Action is successfully executed and the result view is displayed to the user.

2. NONE: indicates that the Action is successfully executed and the view is not displayed to the user.

3. ERROR: indicates that Action execution failed. The ERROR page is displayed to the user.

4. INPUT: indicates that Action requires more INPUT information and returns to the corresponding INPUT page.

5. LOGIN: indicates that the user does not log on correctly but fails to perform the operation correctly. The logon view is returned.

Of course, we can also customize the type of the return value of the Action method. Different result labels will be searched based on the returned values.

2. <result> usage of the type attribute in the tag

You can use the type attribute in <result> to define the output result in different ways. The default value is "dispatcher". Below are some common configurations of the type attribute:

1. dispatcher: the request is forwarded to the specified URL.

2. redirect: the request is redirected to the specified URL.

3. redirectAction: requests are redirected to the specified Action.

4. chain: Action chain processing, forward the request to the specified Action

5. json: return a JSON object when Ajsx is implemented.

4. Dynamic result Configuration

Now, let's consider one question: for example, a project can be divided into two types: Administrator and common user. If the Administrator successfully logs on to the background management page, the common user will jump to the homepage after logging on to the background management page, for example, when <result> is configured, The result page cannot be determined. What should I do if it needs to be determined during running?

Let's take a look at this method to solve this problem:

Package action; import com. opensymphony. xwork2.ActionSupport; import entity. user; public class DynamicAction extends ActionSupport {private User user; // the next jump to the Actionprivate String nextDispatch; public String login () {if (User. getPassword (). equals ("common") {nextDispatch = "common"; return SUCCESS;} else if (user. getUsername (). equals ("admin") {nextDispatch = "admin"; return SUCCESS;} else {return INPUT ;}} public User getUser () {return user ;} public void setUser (User user) {this. user = user;} public String getNextDispatch () {return nextDispatch;} public void setNextDispatch (String nextDispatch) {this. nextDispatch = nextDispatch ;}}

Configuration in web. xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="user" namespace="/user" extends="struts-default"><action name="login" class="action.UserAction" method="login"><result name="success" type="redirectAction">${nextDispatch}</result></action>                   <action name="common" class="action.CommonAction">               <result  name="success">common.jsp</result>           </action>           <action name="admin" class ="action.AdminAction">                <result name="success">admin.jsp</result>            </action>    </package></struts>

In this configuration file, use $ {nextDispatch} to retrieve the value of the nextDispatch variable stored in UserAction, and then based on its value, use the redirectAction method to redirect to <action> whose name is common or admin.

5. configure global results in struts. xml

Configure the <result>

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="user" namespace="/user" extends="struts-default">          <global-results>                   <result name="error">/error.jsp</result>           </global-results><action name="add" class="action.UserAction" method="add"><result >addUser.jsp</result></action>    </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.