2014-10-31 213 views
1

我已經通過了同樣的問題的答案,但沒有任何默認的方式在防止重新提交?另外,在我的情況下,如果我重置表單字段它仍然保存舊值(但它是如何得到這些值)?struts - 防止刷新重新提交

我有一個調度動作類爲:

public class QrcAction extends DispatchAction 
{ 
    public ActionForward waiverPage(final ActionMapping inMapping, 
            final ActionForm inForm, 
            final HttpServletRequest inRequest, 
            final HttpServletResponse inResponse) 
    { 
     QrcForm qrcForm = (QrcForm) inForm; 
     if (StringUtils.isEmpty(qrcForm.getCustomerId())) 
     { 
      ActionMessages errors = getErrors(inRequest); 
      errors.add(IAppConstants.APP_ERROR, new ActionMessage(IPropertiesConstant.ERROR_INVALID_CUSTOMER_ID)); 
      saveErrors(inRequest, errors); 
      return inMapping.findForward(IActionForwardConstant.QRC_SEARCH_CUSTOMER_PAGE); 
     } 
     qrcForm.setCustWaiverPojo(new CrmCustWaiverPojo()); 
     qrcForm.setRemarksPojo(new RemarksPojo()); 
     return inMapping.findForward(IActionForwardConstant.WAIVER_PAGE); 
    } 

    public ActionForward applyWaiver(final ActionMapping inMapping, 
             final ActionForm inForm, 
             final HttpServletRequest inRequest, 
             final HttpServletResponse inResponse) 
    { 
     String forward = IActionForwardConstant.WAIVER_PAGE; 
     QrcForm qrcForm = (QrcForm) inForm; 
     ActionMessages messages = getMessages(inRequest); 
     ActionMessages errors = getErrors(inRequest); 
     CrmuserDetailsDto userDto = (CrmuserDetailsDto) inRequest.getSession(false) 
       .getAttribute(IAppConstants.CRM_USER_OBJECT); 

     double waiverLimit = 0; 
     if (StringUtils.isNotEmpty(userDto.getWaiverLimitAmmount())) 
      waiverLimit = Double.parseDouble(userDto.getWaiverLimitAmmount()); 
     if (waiverLimit < qrcForm.getCustWaiverPojo().getWaiverAmount().doubleValue()) 
     { 
      errors.add(IAppConstants.APP_ERROR, new ActionMessage(IPropertiesConstant.ERROR_INVALID_WAIVER_LIMIT, 
                    waiverLimit)); 
     } 
     QrcFormHelper.validateWaiver(errors, qrcForm); // validates pojo fields 

     if (errors.isEmpty()) 
     { 
      // saving data to database 
      if (success) 
      { 
       messages.add(IAppConstants.APP_MESSAGE, new ActionMessage("success.msg.key")); 
       // resetting the form fields 
       qrcForm.setCustWaiverPojo(new CrmCustWaiverPojo()); 
       qrcForm.setRemarksPojo(new RemarksPojo()); 
       qrcForm.setSrTicketNo(null); 
      } 
      else 
       errors.add(IAppConstants.APP_ERROR, new ActionMessage("error.msg.key")); 
     } 
     saveErrors(inRequest, errors); 
     saveMessages(inRequest, messages); 
     return inMapping.findForward(forward); 
    } 
} 

waiverPage()被稱爲顯示形式並提交給applyWaiver()。在applyWaiver()成功保存值後,我將重置表單域並將其轉發到同一頁面。提交併成功後,將顯示一條消息並清除表單,之後如果再次刷新,則使用與以前相同的值輸入數據庫。
這個問題能解決嗎?

更新
waiver.jsp

<html:form action="manageQrc.do?method=applyWaiver"> 
    <html:hidden property="custWaiverPojo.customerId" name="qrcForm" value="${ qrcForm.customerId }"/> 
    <label class="label_radio"> 
     <html:radio property="custWaiverPojo.waiverType" value="Goodwill Wavier" name="qrcForm" styleClass="waiverType">Goodwill</html:radio> 
    </label> 
    <label class="label_radio"> 
     <html:radio property="custWaiverPojo.waiverType" value="Process Wavier" name="qrcForm" styleClass="waiverType">Process</html:radio> 
    </label> 
    <strong> Select Category </strong> 
    <span class="LmsdropdownWithoutJs"> 
     <html:select property="custWaiverPojo.waiverHead" name="qrcForm" styleId="waiverHead"> 
      <html:option value="">Please Select</html:option> <!-- options are filled after radio button selection --> 
     </html:select> 
    </span> 
    <strong>Amount</strong> 
    <html:text property="custWaiverPojo.waiverAmount" name="qrcForm" styleClass="textbox" styleId="waiverAmount" maxlength="10"></html:text> 
    <strong>Bill No. </strong> 
    <span class="LmsdropdownWithoutJs"> 
     <html:select property="custWaiverPojo.billNo" name="qrcForm" styleId="waiverBill"> 
      <html:option value="0">Please Select</html:option> 
      <html:option value="20140101">20140101</html:option> 
      <html:option value="20140201">20140201</html:option> 
      <html:option value="20140301">20140301</html:option> 
      <html:option value="20140401">20140401</html:option> 
     </html:select> 
    </span> 
    <strong>Ticket ID</strong> 
    <html:text property="srTicketNo" name="qrcForm" maxlength="20" styleClass="textbox" styleId="waiverSRT"></html:text> 
    <strong> Remarks</strong> 
    <html:textarea property="remarksPojo.remarks" name="qrcForm" styleClass="LmsRemarkstextarea" styleId="waiverRemarks"></html:textarea> 
    <html:submit /> 
</html:form> 
+0

http://en.wikipedia。組織/維基/後/重定向/獲取。 – 2014-10-31 11:08:22

+0

@AleksandrM:感謝您的輸入,但是在我的場景中,我無法使用[**'PRG' **](http://en.wikipedia.org/wiki/Post/Redirect/Get) – 2014-10-31 11:49:24

回答

1

在此blog的解決方案使用令牌來識別請求的行動正向:使用
保存會話令牌:

第1步在Action class中實施的saveToken()方法。

public class EmployeeLoadAction extends Action{ 
    private final static String SUCCESS = "success"; 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) 
    throws Exception { 
     ActionForward forward; 
     forward = mapping.findForward(SUCCESS); 
     saveToken(request); 
     return forward; 
    } 
} 

步驟2:
在JSP文件。在此例中爲employee.jsp
如下所示,使用JSP文件中的隱藏變量存儲令牌。 不要忘了導入JSP文件中GlobalsConstants類,以避免Jasper Exception

TRANSACTION_TOKEN_KEY變量中Action class被棄用,因此,從Globlas class用它代替。

<%@ page import="org.apache.struts.Globals" %> 
<%@ page import="org.apache.struts.taglib.html.Constants" %> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>Insert title here</title> 
    </head> 
    <body> 
     <form name="employee" action="EmployeeSubmit.do" method="POST"> 
     <input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Globals.TRANSACTION_TOKEN_KEY) %>"> 
     <TABLE> 
      <TR> 
      <TD>Name</TD> 
      <TD> 
       <input type="text" name="empName"> 
      </TD> 
      </TR> 
      <TR> 
      <TD>ID</TD> 
      <TD> 
       <input type="text" name="empId"> 
      </TD> 
      </TR> 
      <TR> 
      <TD colspan="2"> 
       <input type="submit" value="Submit"> 
      </TD> 
      </TR> 
     </TABLE> 
     </form> 
    </body> 
</html> 

第3步:
現在實際的邏輯裏面EmployeeSubmit動作類重複提交。 方法isTokenValid()將驗證令牌並返回boolean。 基於此,我們可以決定表單是否已重新提交。

public class EmployeeSubmitAction extends Action{ 
    private final static String SUCCESS = "success"; 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) 
    throws Exception { 
     ActionForward forward; 
     forward = mapping.findForward(SUCCESS); 
     EmployeeSubmitForm frm = (EmployeeSubmitForm) form; 
     if (isTokenValid(request)) { 
      System.out.println("frm.getName() : " + frm.getEmpName()); 
      resetToken(request); 
     } else { 
      System.out.println("frm.getName() : " + frm.getEmpName()); 
      System.out.println("Duplicate Submission of the form"); 
     } 
     return forward; 
    } 
} 
0

在上保存成功值applyWaiver()我重置表單字段並將其發送到同一個頁面。

這是一個問題,因爲在提交之後,您應該重定向到會顯示一個頁面的操作。在動作中,如果不是會話範圍,也可以填充表單bean。所以,在代碼中使用重定向到

public ActionForward applyWaiver(final ActionMapping inMapping, 
            final ActionForm inForm, 
            final HttpServletRequest inRequest, 
            final HttpServletResponse inResponse) 
{ 
    ... 

    if (! errors.isEmpty()) 
    { 
     errors.add(IAppConstants.APP_ERROR, new ActionMessage("error.msg.key") ; 

     // resetting the form fields 
     qrcForm.setCustWaiverPojo(new CrmCustWaiverPojo()); 
     qrcForm.setRemarksPojo(new RemarksPojo()); 
     qrcForm.setSrTicketNo(null); 

     saveErrors(inRequest, errors); 
     saveMessages(inRequest, messages); 
     return inMapping.findForward(forward); 
    } 

    MessageResources mr = (MessageResources) inRequest.getAttribute(Globals.MESSAGES_KEY); 
    // saving data to database 
    if (success) 
     inRequest.setAttribute(IAppConstants.APP_MESSAGE, mr.getMessage("success.msg.key")); 
    else 
     inRequest.setAttribute(IAppConstants.APP_MESSAGE, mr.getMessage("error.msg.key")); 

    return new ActionForward(inMapping.findForward(forward).getPath(), true); //true is for redirect 
} 
+0

仍然無法使用。提交以前的值。 – 2014-10-31 12:26:21

+0

提交具有表單的JSP已提交。 – 2014-10-31 12:33:21

+0

請參閱更新 – 2014-10-31 12:56:47