struts2学习(4)struts2核心知识III

一、result配置:                                    

result - name 就是前面返回的值,success,error等。
type:
dispatcher. 默认是这个,底层是jsp的forward;
redirect:重定向;
chain:比如在aAction里面请求的东西,返回的时候到另一个Action里面继续执行,并且数据可以共享;相当于内部转发到了一个新的Action
redirectAction:重定向到一个Action;例子中从hello action中的数据,重定向到hello2,这里面的数据是带不过去的(name的值带不过去);
freemarker:一种模板技术;
velocity:也是一种模板;
 
 

1.result中的type类型:                        

com.cy.action.HelloAction.java:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    //redirect
    public String r() throws Exception {
        return "r";
    }
    
    //chain
    public String c() throws Exception {
        return "c";
    }
    
    //redirectAction
    public String ra() throws Exception {
        return "ra";
    }
    
}
View Code

com.cy.action.HelloAction2.java:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction2 extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private String name2;

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    @Override
    public String execute() throws Exception {
        this.name2 = "你好啊";
        return SUCCESS;
    }
    
}
View Code

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>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    
    <package name="manage" namespace="/" extends="struts-default">
        <action name="hello" class="com.cy.action.HelloAction">
            <result name="success" type="dispatcher">success.jsp</result>
            <result name="r" type="redirect">success.jsp</result>
            <result name="c" type="chain">hello2</result>
            <result name="ra" type="redirectAction">hello2</result>
        </action>
        
        <action name="hello2" class="com.cy.action.HelloAction2">
            <result name="success" type="dispatcher">success.jsp</result>
        </action>
    </package>
</struts>

index.jsp:

<body>
    <a href="hello?name=struts2" target="_blank">默认转发_dispatcher</a><br>
    <a href="hello!r?name=struts2" target="_blank">重定向_redirect</a><br>
    <a href="hello!c?name=struts2" target="_blank">链条_chain</a><br>
    <a href="hello!ra?name=struts2" target="_blank">重定向到Action_redirectAction</a><br>
</body>

success.jsp:

<body>
    name: ${name}<br>
    name2:${name2 }
</body>

测试结果:

1)默认转发-dispatcher:

name的值取到了;url中地址不变;

2)重定向-redirect:

name的值取不到;不是同一个request了,所以数据没法共享;

url地址栏变了;

3)链条-chain:

name的数据能取,内部转发到了一个新的Action,这个数据可以共享;

name2的数据能取,是hello2Action自己设进去的,肯定能取;

url地址:不变;

4)重定向到action - redirectAction:

name的值取不到:从一个action中重定向到另一个action继续执行,这里面数据带不过去,不是一个request;客户端重新请求hello2.action:那么:

name2的值是hello2Action自己设进去的,肯定能取;

url:已经变成了hello2.action;说明客户端重新请求了hello2.action;

2.result全局配置:                                                    

比如result为error,几乎是所有的action都要共用到的;这时我们就可以把它配置为全局的;
 
执行过程:
首先在hello的action配置中找result name为error的;
如果没有找到,就去global-result中找;
如果hello action中已经有了,就不会到其他地方去找了;
 
下面是例子:
HelloAction.java:
package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private String name;
    private String error;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        if(name==null || name.length()==0){
            error = "name是空的";
            return ERROR;
        }
        return SUCCESS;
    }
    
    //redirect
    public String r() throws Exception {
        return "r";
    }
    
    //chain
    public String c() throws Exception {
        return "c";
    }
    
    //redirectAction
    public String ra() throws Exception {
        return "ra";
    }
    
}
View Code

struts.xml配置:

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    
    <package name="manage" namespace="/" extends="struts-default">
        <global-results>
            <result name="error">error.jsp</result>
        </global-results>
            
        <action name="hello" class="com.cy.action.HelloAction">
            <result name="success" type="dispatcher">success.jsp</result>
            <result name="r" type="redirect">success.jsp</result>
            <result name="c" type="chain">hello2</result>
            <result name="ra" type="redirectAction">hello2</result>
        </action>
        
        <action name="hello2" class="com.cy.action.HelloAction2">
            <result name="success" type="dispatcher">success.jsp</result>
        </action>
    </package>
</struts>

error.jsp:

<body>
    错误信息:${error }
</body>

index.jsp:

<body>
    <a href="hello" target="_blank">全局result配置</a><br>
</body>

测试:

-------------

原文地址:https://www.cnblogs.com/tenWood/p/7096992.html