Struts2零配置
六、常用注解
1.@Action注解
①标记在Action方法上,指定访问当前Action方法的URL地址
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;
public class HelloWorld extends ActionSupport { @Action("/different/url") public String execute() { return SUCCESS; } } |
②与@Actions注解配合,为当前Action方法匹配多个URL地址
public class HelloWorld extends ActionSupport { @Actions({ @Action("/different/url"), @Action("/another/url") }) public String execute() { return SUCCESS; } } |
③标记在Action方法上,定义execute()以外的Action方法
public class HelloWorld extends ActionSupport { @Action("/different/url") public String execute() { return SUCCESS; }
@Action("url") public String doSomething() { return SUCCESS; } }
|
④指定当前Action方法引用的拦截器或拦截器栈
@Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")}) public String execute() { return SUCCESS; } @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true})) public String execute() { return SUCCESS; } |
2.Result注解
①为Action类声明类级别的result
@Results({ @Result(name="failure", location="fail.jsp") }) public class HelloWorld extends ActionSupport { @Action(value="/different/url", results={@Result(name="success", location="http://struts.apache.org", type="redirect")} ) public String execute() { return SUCCESS; }
@Action("/another/url")
public String doSomething() { return SUCCESS; } } |
②指定结果类型并设置参数
@Action(value="/different/url", results={@Result(name="success", type="httpheader", params={"status", "500", "errorMessage", "Internal Error"})} ) public String execute() { return SUCCESS; } |
本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。