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

本文介绍如何在Struts2中使用内置的token标签及拦截器来预防表单的重复提交,通过简单的两步配置即可有效避免刷新或后退导致的数据误提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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 内置过滤器进行防止后退重复提交的方式。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值