1. 服务器处理慢,用户手动再次点击了提交相关的按钮
2. 提交成功后,用户因为某些原因进行刷新操作
解决方案:
1. 表单提交方法应该设置为method="post" 这样用户刷新时,浏览器会提示用户是否再次提交(method="get"则不会)
2. 在表单数据提交后,程序处理应该使用重定向页面,这样刷新也重复提交不了数据了
3. 使用struts2 的token机制来拦截重复提交
使用token的具体步骤如下:
1. 页面表单内增加如下代码:
<s:token></s:token>或<s:token/>
2. 于struts.xml中(package中)配制拦截器:
<interceptors> <!-- 声明需要的拦截器 --> <interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor"/> <interceptor-stack name="myStack"> <!-- 加入队列的拦截器 --> <interceptor-ref name="token"/> <interceptor-ref name="alias" /> <interceptor-ref name="basicStack"/> <!--必需引用这个,否则点下一个子ACTION会报错--> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <!-- 配置默认的拦截器 此处因为我是做测试,实际应该于action中配制使用,不应该全用默认包方式--> <default-interceptor-ref name="myStack" />
3. 在struts.xml处理的 action 中增加,用户重复提交的处理页面.
<action name="login" method="login" class="com.test.action.LoginAction"> <result name="input">/login.jsp</result> <result name="success">/index.jsp</result> <!-- 配制拦截后跳转的页面 --> <result name="invalid.token">/invalidToken.jsp</result> <result name="exception">/error.jsp</result> <!-- 配制定义的拦截器 --> <interceptor-ref name="myStack" /> <!-- 配制异常信息处理,指Action抛出Exception异常时,转入名为exception的结果页面 --> <exception-mapping result="exception" exception="Exception" /> </action>