BlogJava 联系 聚合 管理  

Blog Stats

随笔档案


bitmap

bitmap

问题的产生原因
       页面提交给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,进入目标页,如 http://www.bt285.cn ,通过IndexAction转发,其中需要保存令牌:

public ActionForward execute(ActionMapping mapping, ActionForm form,   
        HttpServletRequest request, HttpServletResponse response)   
        
throws Exception {   
    saveToken(request);   
    
return mapping.findForward("index");// http://www.5a520.cn   
}
  
 2,目标页面(test.jsp)需要使用Struts标签库:
<body>  
    
<html:form action=" http://www.5a520.cn test.do" method="post">  
        
<input type="submit" value="提交" />  
    
</html:form>  
</body>  
当提交test.jsp时提交到TestAction处理:
<struts-config>  
    
<form-beans>  
        
<form-bean name="testForm" type="com.tanlan.struts.form.TestForm">  
        
</form-bean>  
    
</form-beans>  
    
<action-mappings>  
        
<action path="/index" type="com.tanlan.struts.action.IndexAction">  
            
<forward name="index" path="/index.jsp"></forward>  
        
</action>  
        
<action path="/test" type="com.tanlan.struts.action.TestAction"  
            name
="testForm" input="/index.jsp">  
            
<forward name="test" path="/test.jsp"></forward>  
            
<forward name="error" path="/error.jsp"></forward>  
        
</action>  
    
</action-mappings>  
</struts-config>  

至此,大功告成!

完整示例请参考附件。

 

posted on 2009-07-12 12:55 bitmap 阅读(1694) 评论(0)  编辑  收藏