当struts2的Action用Convention插件实现的时候,interceptor也就相应地需要用到Annotation来注解了。
1、interceptor需要在struts.xml中完成配置,struts2中的convention插件暂时没有对interceptor的映射支持。需要注意的问题是:当你手动配置interceptor的时候,struts2默认配置的interceptor将不会实现,所以,如果需要用到,就要自己手动将其配置上。
<interceptors>
<interceptor name="simple" class="com.action.interceptor.SimpleInterceptor"></interceptor>
<interceptor-stack name="myDefault">
<interceptor-ref name="simple"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
2、通过注解为Action配置上interceptor。
a)为Action配置上interceptor。
b)为方法配置上interceptor。
当为Action配置上全局的interceptor时,只需要把@InterceptorRefs(...)配置在类上。
@InterceptorRefs({
@InterceptorRef("myDefault")
})
public class TestAction extends ActionSupport{
@Override
@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
public String execute() throws Exception {
return SUCCESS;
}
}
如果只是方法上的interceptor,则只要配置在所需要配置的上的@Action属性中。
public class TestAction extends ActionSupport{
@Override
@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
public String execute() throws Exception {
return SUCCESS;
}
}
interceptor源码如下:
package com.action.interceptor;
import java.util.Date;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SimpleInterceptor
extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation)
throws Exception {
long start = System.currentTimeMillis();
String result = invocation.invoke();
long end = System.currentTimeMillis();
System.out.println( name + " this interceptor method **********"
+ " over this request spend time = " + (end - start) + " ms ");
return result;
}
@Override
public void init() {
System.out.println("SimpleInterceptor init ***********");
super.init();
}
@Override
public void destroy() {
System.out.println("SimpleInterceptor destroy ***********");
super.destroy();
}
}
package com.action.user;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("strutsConventionLearning")
@InterceptorRefs({
@InterceptorRef("myDefault")
})
@Results({
@Result(name="error",location="/index.jsp")
})
public class TestAction extends ActionSupport{
private String name;
private String password;
@Override
@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
public String execute() throws Exception {
System.out.println("convertion test result is true");
System.out.println("name = " + this.getName() + "*******");
System.out.println("password = " + this.getPassword() + "*******");
if(this.getName().length() < 2)
return ERROR;
else
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<package name="strutsConventionLearning" extends="struts-default">
<interceptors>
<interceptor name="simple" class="com.action.interceptor.SimpleInterceptor"></interceptor>
<interceptor-stack name="myDefault">
<interceptor-ref name="simple"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>