来贴个代码:
<struts>
<!-- 动态方法盗用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<!-- struts容器检索value中出现的包及其子包下所有的类,将这些类视作Action -->
<constant name="struts.convention.package.locators" value="web,action" />
<!-- 设置结果页面的根目录 ,action中写"/",访问外部页面,不写"/",以 value="/WEB-INF/page"为起点开始访问内部页面-->
<constant name="struts.convention.result.path" value="/WEB-INF/page" />
<package name="myPackage" namespace="/test" extends="struts-default">
<interceptors>
<interceptor name="secondInter" class="com.su.interceptor.SecondInterceptor"></interceptor>
<interceptor name="firstInter" class="com.su.interceptor.FirstInterceptor"></interceptor>
</interceptors>
</package>
</struts>
@ParentPackage(value="myPackage") //struts.xml文件包的名字
@Namespace(value="/test") //Namespace的路径
public class FirstAction extends ActionSupport {
//value值 是表单提交的action名字
@Action(value="one",results={@Result(location="success.jsp",name="success",type="dispatcher"),
@Result(location="/error.jsp",name="error",type="redirect")})
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("in FirstAction method execute()");
return "success";
}
@Action(value="two",results=@Result(location="success.jsp"),
interceptorRefs={@InterceptorRef(value="firstInter"),@InterceptorRef(value="secondInter")})
public String runOne() throws Exception {
// TODO Auto-generated method stub
System.out.println("in FirstAction method runOne()");
return "success";
}
@Action(value="three",results={@Result(location="success.jsp"),@Result(location="nullerror.jsp",name="nullerror")},
exceptionMappings={@ExceptionMapping(exception="java.lang.NullPointerException",result="nullerror"),@ExceptionMapping(exception="java.io.IOException",result="ioerror.jsp")})
public String runTwo() throws Exception {
// TODO Auto-generated method stub
System.out.println("in FirstAction method runTwo()");
throw new NullPointerException();
//return "success";
}
}