Struts2 Token的简单应用

在Struts2中防止页面重复提交,非常简单。

1:首先在页面端放置一个标签<s:token/>

2:在Struts.xml里配置这样两句代码

    第一句:定义一个结果映射

   

<result name="invalid.token">/WEB-INF/pages/register.jsp</result>

   第二句:定义一个token的拦截器,避免拦截正常的方法

   

<interceptor-ref name="token">
     <param name="excludeMethods">default</param>
</interceptor-ref>

   这样页面再刷新提交,就不起作用了。

   其实现原理是对于<s:token/>标签,Struts会把它翻译为:

  

<input type="hidden" name="struts.token.name" value="xxx"/>
<input type="hidden" name="xxx" value="一串加密过的令牌值"/>

  然后在org.apache.struts2.interceptor.TokenInterceptor里的doIntercept()方法里进行比较,它返回的结果代码是一个常量INVALID_TOKEN_CODE="invalid.token",所以需要配置相应的结果映射。

  另可参加:http://bestchenwu.iteye.com/admin/blogs/1025597

猜你喜欢

转载自bestchenwu.iteye.com/blog/1025657