`

struts2中Action跳转的convention 配置

 
阅读更多
本action演示了一个action执行完成后,转到另外一个action的情况。
(1)如果需要内部转发:则在要转到的方法前面定义一个新的action名称,名称为:前一方法名!前一方法返回值.
(2)如果需要外部重定向:则定义一个Result,类型必须为"redirect"。
(3)若即定义了外部重定向Result,也定义了内部转发,显调用时的名称相同,则执行外部重定向Result
package com.abc.domain.access.action.common;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
//声明此类为控制层的类,且为prototype模式调用
@ParentPackage(value = "abc-default")
@Results(
{
@Result(name="toShow", location="show.action", type="redirect"),
@Result(name="add", location="add.action", type="redirect")
})


@Namespace("/common/test")
public class TestAction
{

@Actions({
   @Action("show"),
   @Action("one!show")
   }) 
public String show() throws Exception
{  
   return "success";
}

@Actions({
   @Action("add"),
   @Action("two!add")
   }) 
public String add() throws Exception
{  
   return "success";
}

@Action("one")
public String one() throws Exception
{  
  //return "show";   //内部转发到show(),找到名为"one!show"的Action执行
   return "toShow"; //外部重定向到show(),找到名为"toShow"的Result,然后再执行show()
}

@Action("two")
public String two() throws Exception
{  
return "add"; //外部重定向到add(),因为找到了名称为add的Result配置,所以不再去找two!add
}
}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics