`
wangrl
  • 浏览: 148208 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2 中使用token标签防止刷新和后退进行重复提交

 
阅读更多

在Struts2中使用token标签和内置的拦截器,来防止刷新提交和后退提交非常简单方便。

1. 在需要防止重复提交的jsp中,只需添加一个标签<s:token />

2. 在对应的struts.xml中进行配置token拦截器,如下代码,红色部分

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="cinema" extends="struts-default">
<global-results>
<result name="error">/index.jsp</result>
</global-results>

<action name="cinema" class="cinemaAction">
<result name="saveSuccess" type="redirect-action">cinema!listCinema</result>
<result name="listSuccess">cinema/listCinema.jsp</result>
<result name="deleteSuccess" type="redirect-action">cinema!listCinema</result>
<result name="toUpdate">cinema/updateCinema.jsp</result>
<result name="updateSuccess" type="redirect-action">cinema!listCinema</result>
<result name="toAddSuccess">cinema/addCinema.jsp</result>
<result name="toAddScreen" type="redirect-action">screen!preAddScreen</result>

<result name="invalid.token" type="redirect-action">cinema!listCinema</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="token">
<!-- includeMethods表示包含指定的方法,即对标记为includeMethods的方法进行拦截 -->
<param name="includeMethods">saveCinema,saveCinemaAndtoAddScreen,updateCinema</param>

<!-- 定义被排除的方法名,也就是你action中不被这个拦截器拦截的方法名 -->
<param name="excludeMethods"></param>
-->
</interceptor-ref>

</action>

</package>
</struts>

总结:以上两步即完成了使用Struts2 内置过滤器进行防止后退重复提交的方式。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics