解决Struts重复提交

关键字: struts 重复提交

问题的产生原因
       页面提交给Action去进行业务处理,Action再跳转回前台页面,但这时URL依然是“页面提交给Action的链接”,这时前台刷新一下页面,就变成再次执行了一次提交操作。

 

解决思路
     1,在Action页面中跳转的时候用重定向,可以在struts-config.xml中配置<forward ... redirect=“true”>
不过这种方法会使得request中放置数据丢失;
     2,用Token令牌环来实现
          提交到Action的时候,进行一系列操作,然后保存一个标志,这时再跳转到前台页面。如果前台页面刷新的话,Action通过查看是否有标志,就能判断用户是刷新还是提交。

 

Action中操作令牌的方法
      一: saveToken(HttpServletRequest request)
            创建一个新令牌,并将其保存在当前用户的会话中,如果用户的会话不存在,将首先创建新会话对象
      二: isTokenValid(HttpServletRequest request)
            判断存储在当前会话中的令牌值和请求参数中的令牌值是否匹配,如果匹配,返回true,否则返回false 。
            以下情况返回false:
                  1,用户的HttpSession不存在
                  2,用户Session中没有保存令牌值
                  3,在用户请求参数中没有令牌值
                  4,存储在用户Session范围内的令牌值和请求参数中 的令牌值不匹配
       三:resetToken()
             删除保存在Session范围内的令牌值

 

示例代码

1,进入目标页,通过IndexAction转发,其中需要保存令牌:

Java代码 复制代码
  1. public ActionForward execute(ActionMapping mapping, ActionForm form,   
  2.         HttpServletRequest request, HttpServletResponse response)   
  3.         throws Exception {   
  4.     saveToken(request);   
  5.     return mapping.findForward("index");   
  6. }  
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		saveToken(request);
		return mapping.findForward("index");
	}

 2,目标页面(test.jsp)需要使用Struts标签库:

Html代码 复制代码
  1. <body>  
  2.     <html:form action="test.do" method="post">  
  3.         <input type="submit" value="提交" />  
  4.     </html:form>  
  5. </body>  
	<body>
		<html:form action="test.do" method="post">
			<input type="submit" value="提交" />
		</html:form>
	</body>

 3,当提交test.jsp时提交到TestAction处理:

Java代码 复制代码
  1. public ActionForward execute(ActionMapping mapping, ActionForm form,   
  2.         HttpServletRequest request, HttpServletResponse response)   
  3.         throws Exception {   
  4.     /* 如果令牌有效,则执行业务逻辑 */  
  5.     if (isTokenValid(request)) {   
  6.         /* 删除令牌 */  
  7.         resetToken(request);   
  8.         return mapping.findForward("test");   
  9.     }   
  10.     return mapping.findForward("error");   
  11. }  
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		/* 如果令牌有效,则执行业务逻辑 */
		if (isTokenValid(request)) {
			/* 删除令牌 */
			resetToken(request);
			return mapping.findForward("test");
		}
		return mapping.findForward("error");
	}

 

4,struts-config.xml配置:

Xml代码 复制代码
  1. <struts-config>  
  2.     <form-beans>  
  3.         <form-bean name="testForm" type="com.tanlan.struts.form.TestForm">  
  4.         </form-bean>  
  5.     </form-beans>  
  6.     <action-mappings>  
  7.         <action path="/index" type="com.tanlan.struts.action.IndexAction">  
  8.             <forward name="index" path="/index.jsp"></forward>  
  9.         </action>  
  10.         <action path="/test" type="com.tanlan.struts.action.TestAction"  
  11.             name="testForm" input="/index.jsp">  
  12.             <forward name="test" path="/test.jsp"></forward>  
  13.             <forward name="error" path="/error.jsp"></forward>  
  14.         </action>  
  15.     </action-mappings>  
  16. </struts-config>  

 

至此,大功告成!

完整示例请参考附件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值