struts2防止反复提交的办法

<?

xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <!-- 这里是拦截器的配置 --> <interceptors> <interceptor name="test" class="com.test.interceptor.TestInterceptor"></interceptor> <!-- 用于仅拦截某些方法 --> <interceptor name="function" class="com.test.interceptor.FunctionInterceptor"> <param name="includeMethods">add</param> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="test"/> <interceptor-ref name="function"/> <interceptor-ref name="token"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <!-- 正确和错误的分别跳转。input是在验证失败后会跳转的页面 --> <action name="data" class="com.test.action.ValideAction"> <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用拦截器的动作 --> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> <result name="invalid.token">/token_error.jsp</result><!-- 防止反复提交的办法 --> </action> <action name="FunctionInterceptor" class="com.test.action.ValideAction" method="add"> <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用拦截器的动作 --> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> <result name="invalid.token">/token_error.jsp</result> </action> <action name="dele" class="com.test.action.ValideAction" method="dele"> <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用拦截器的动作 --> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> <result name="invalid.token">/token_error.jsp</result> </action> </package> </struts>

上面是struts.xml中的代码,基本的代码是:

 <result name="invalid.token">/token_error.jsp</result>

这样一段。


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 这个地方是用来显示错误信息 -->
<s:fielderror/>
<form action="data.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
-------------------联系方式----------------<br>
手机:<input type="text" name="contact.phone"><br>
地址:<input type="text" name="contact.address"><br>
邮箱:<input type="text" name="contact.email"><br>

<s:token></s:token><!-- 防止反复提交。要加上这一段话 -->
<input type="submit" name="ok"><br><br>


<a href="FunctionInterceptor.action">測试FunctionInterceptor的add</a><br><br>
<a href="dele.action">測试FunctionInterceptor的dele</a><br><br>
</form>
</body>
</html>

在JSP页面中,要加上这样一段话,<s:token></s:token><!-- 防止反复提交。要加上这一段话 -->

就能够防止反复提交了。


原文地址:https://www.cnblogs.com/lxjshuju/p/7353057.html