在页面的提交表单中加入<s:token/>标签,当请求该页面时,由服务器产生一个字符串,如:LO3ZSJ4MGAQH3VMFS425V62PK535THUI。可以通过查看ie源码查看生成的字符串。服务器产生这个字符串后将其放入到服务器的session里。同时客户端的字符串将随着提交,token拦截器在action执行之前会比较那个字符串与服务器session的字符串是否一致,比较一次后session中的字符串将会被删除。如果用户重复提交将不会通过。
Form token LO3ZSJ4MGAQH3VMFS425V62PK535THUI does not match the session token null.
这个提示是当用户重复提交时,后台提示的信息。它说的是表达令牌LO3ZSJ4MGAQH3VMFS425V62PK535THUI与session中的空令牌不匹配,说明用户已经重复提交了。当重复提交时,token拦截器会返回一个invalid.token。
在action的result里必须配置一个
<result name="invalid.token">/error.jsp</result>
自定义拦截器:
//MyIntercptor.java这个拦截器的作用是计算action执行过程所花费的时间。
package com.xie.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class MyInterceptor implements Interceptor{
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
long startTime=System.currentTimeMillis();
//调用invoke
String r=invocation.invoke();
long endTime=System.currentTimeMillis();
System.out.println("time="+(endTime-startTime));
return r;
}
}
//struts.xml中配置:
<package name="interceptor" namespace="/" extends="struts-default">
//指定自定义拦截器类
<interceptors>
<interceptor name="timeinterceptor" class="com.xie.interceptor.MyInterceptor"></interceptor>
</interceptors>
<default-action-ref name="index" />
<action name="index" >
<result>/index.jsp</result>
</action>
<action name="test" class="com.xie.interceptor.InterceptorAction">
<result>/test.jsp</result>
//使用自定义的拦截器,注意应该加上defaultStack
<interceptor-ref name="timeinterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>