`
shiwj1010
  • 浏览: 97335 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

struts1.x 防止表单重复提交

    博客分类:
  • j2ee
阅读更多
走一下action流程再到要防止重复提交的页面
public class PrepareTokenAction extends Action {
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        // Generate a unique token that will be
        // check when the form is submitted
        saveToken(request);


        // Forward to the form
        return mapping.findForward("success");

    }
}


处理提交页面的action
public class ProcessTokenAction extends Action {
public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        // If user pressed 'Cancel' button,
        // return to home page
        if (isCancelled(request)) {
            return mapping.findForward("home");
        }

        ActionErrors errors = new ActionErrors();

        // Prevent unintentional duplication submissions by checking
        // that we have not received this token previously
        if (!isTokenValid(request)) {
            errors.add(
                ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("errors.token"));
        }
        resetToken(request);

        // Report any errors we have discovered back to the original form
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
            saveToken(request);
            return (mapping.getInputForward());
        }

        // Forward to result page
        return mapping.findForward("success");
    }

}
分享到:
评论
2 楼 shiwj1010 2009-06-22  
看看能否这样解决呢?在前台用javascript防止连续按2次提交按钮。提交后按钮变灰,行不?
1 楼 zalbelieve 2009-06-21  
老友:我现在的情况是,能防止表单重复提交.但是,如果按后退键后, 输入相同的内容, 按提交按钮, 提示表单重复提交, 可是如果按两次提交按钮的时候[输入相同的内容], 郁闷啦, 居然提交成功啦 超级郁闷 希望能给点原因 谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics