action 交互到帶命名空間的action 還有返回的jsp

代碼:
不帶命名空間的2個action的交互,

package com.act;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("struts-default")
public class reTest1 extends ActionSupport {
@Override
public String execute() throws Exception {
    // TODO Auto-generated method stub
    return super.execute();
}
@Action(value="res_1",results={@Result(type="redirectAction",location="res_2")})
public String res_1() throws Exception {
    System.out.println("res_1------------->>");
    return SUCCESS;
}
@Action(value="res_2",results={@Result(type="redirectAction",location="res_1_1",params={"namespace","/test"})})
public String res_2() throws Exception {
    System.out.println("res_2------------->>");
    return SUCCESS;
}
}

然後由第二個action重定向到帶命名空間的action
那就需要params={"namespace","/test"}配上這個
或者將地址改成如下:

location="test/res_1_1"
package com.act;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("struts-default")
@Namespace("/test")
public class reTest2 extends ActionSupport {
    @Action(value="res_1_1",results={@Result(type="redirectAction",location="res_2_2")})
    public String res_1() throws Exception {
        System.out.println("res_1_1------------->>");
        return SUCCESS;
    }
    @Action(value="res_2_2",results={@Result( type="redirect",location="../res_1.jsp")})
    public String res_2() throws Exception {
        System.out.println("res_2_2------------->>");
        return SUCCESS;
    }
}

兩個JSP沒貼。沒什麼
我的工程配置了/路徑的。
至於JSP的返回../res_1.jsp。本來是在test路徑下,然後../就到了根路徑下??大神救我~~
這是返回的結果:
res_1————->>
res_2————->>
res_1_1————->>
res_2_2————->>

下面的代碼是從命名空間傳出來

package com.act;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("struts-default")
public class reTest1 extends ActionSupport {
@Override
public String execute() throws Exception {
    // TODO Auto-generated method stub
    return super.execute();
}
//這裏默認的工作空間是根,我在配置文件裏面配置了一句
@Action(value="res_1",results={@Result(type="redirect",location="res_2.jsp")})
public String res_1() throws Exception {
    System.out.println("res_1------------->>");
    return SUCCESS;
}
@Action(value="res_2",results={@Result(type="redirectAction",location="res_1")})
public String res_2() throws Exception {
    System.out.println("res_2------------->>");
    return SUCCESS;
}
}

帶命名空間的往外傳數據請求

package com.act;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("struts-default")
@Namespace("/test")
public class reTest2 extends ActionSupport {
    @Action(value="res_1_1",results={@Result(type="redirectAction",location="res_2",params={"namespace","/"})})
    public String res_1() throws Exception {
        System.out.println("res_1_1------------->>");
        return SUCCESS;
    }
    @Action(value="res_2_2",results={@Result( type="redirectAction",location="res_1_1")})
    public String res_2() throws Exception {
        System.out.println("res_2_2------------->>");
        return SUCCESS;
    }
}
發佈了26 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章