struts2中使用token避免重复提交
<action name="register" class="org.sunxin.struts2.action.RegisterAction">
<!-- 配置异常映射,当RegisterAction抛出Exception异常时,向用户显示error.jsp页面-->
<exception-mapping result="error" exception="java.lang.Exception"/>
<result name="input">/pages/register.jsp</result>
<result name="success">/pages/success.jsp</result>
<result name="error">/pages/error.jsp</result>
<result name="invalid.token">/pages/register.jsp</result>
<result name="wait">wait.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="token">
<param name="excludeMethods">input</param>
</interceptor-ref>
</action>
2.在页面中加
<s:actionerror/>
<s:form action="register" method="post">
<s:token></s:token>
</s:form>
3.<interceptor-ref name="token"/>
<interceptor-ref name="token-session"/>
<!--注意struts2.0 拦截器名字为token-session struts2.1.2 已经更改为tokenSession -->
token: 在活动中检查合法令牌(token), 防止表单的重复提交; 在<s:actionerror/>会产生提示信息
token-session: 同上, 但是在接到非法令牌时将提交的数据保存在session中; 不会在<s:actionerror/>会产生提示信息
只会在后台发出警告并处理,如下:
警告: Form token KO80SIJW4F84034NG5HM1ZBUGOVNY64D does not match the session token null.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步