Cookbook-struts1.3.8案例分析-Control duplication form submission
Cookbook-struts1.3.8案例分析-Control duplication form submission
l Control duplication form submission
PrepareAction的定義
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"); } |
頁面會自動產生<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="1d91170701cd672e7971deaac008bf81" />
ProcessAction所作的事情
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"); } |
posted on 2008-07-08 02:15 MingIsMe 閱讀(57) 評論(0) 編輯 收藏 所屬分類: 16 案例分析