Struts2 配置过滤器,他跟Result是一个地位,也是在action里面可以配置的
<struts>
<package name="test" extends="struts-default">
<interceptors>
这是自定义的过滤器
<interceptor name="myInter" class="com.interceptor.myInterceptor">
</interceptor>
<interceptor-stack name="defaultStack">
<interceptor-ref name="token" />
<interceptor-ref name="defaultStack"></interceptor-ref>
将自定的过滤器添加到栈里面,不将这个加到栈里面,会出现前段的参数传递不过来
<interceptor-ref name="myInter"></interceptor-ref>
</interceptor-stack>
<!--
这个是我自己定义一个栈,将默认的栈放到我自己定义的栈中
<interceptor-stack name="myStack">
<interceptor-ref name="token" />
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myInter" />
</interceptor-stack> -->
</interceptors>
</package>
</struts>
接受请求的ACtion类
package com.act;
import javax.interceptor.Interceptor;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("test")
public class FilterTestAct extends ActionSupport{
private String msg;
private Integer id;
private String money;
private String msg1;
@Override
//我是用的是默认的defaultStack这个所以我注释了/*,interceptorRefs={@InterceptorRef("myStack")}*/这句代码
@Action(value="test",results={@Result(location="index.jsp")}/*,interceptorRefs={@InterceptorRef("myStack")}*/)
public String execute() throws Exception {
// TODO Auto-generated method stub
msg="Action+done!"+id;
return SUCCESS;
}
@Action(value="pay",results={@Result(location="index.jsp"),@Result(name="invalid.token" ,location="err.jsp")}/*,interceptorRefs={@InterceptorRef("myStack")}*/)
public String pay(){
msg1=money;
return SUCCESS;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getMsg1() {
return msg1;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
}
package com.interceptor;
import java.util.Random;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class myInterceptor extends AbstractInterceptor{
private Random random=new Random();
private int ss=5000;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
long beginTimer=System.currentTimeMillis();
Thread.currentThread().sleep(random.nextInt(5)*1000);
//拦截通过
String relcode=arg0.invoke();
System.out.println("处理时间"+(System.currentTimeMillis()-beginTimer));
//用来判断拦截是否给通过
return relcode;
}
}
然后是传到过滤器里面:
在invoke上面的打印会在action之前处理
在invoke下面的打印会在action之后处理
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习