Spring Thymeleaf两次表单提交错误400

时间:2017-10-18 06:40:36

标签: java spring forms thymeleaf

我有一个百万美元形式,可以通过spring控制器验证来自数据库的数据,数据验证工作正常,但是当我想在验证后验证不同的数据时它会给我一个错误400,没有错误在堆栈跟踪中,所以我不知道如何解决这个问题,可能是因为我的逻辑错误,因为我是新手。

这里的表格是

<form th:name="memberRkiForm" id="memberRkiForm" th:action="@{/}" th:object="${formResult}"
      method="POST">
  <div>
    <table id="form">

      <tr>
        <td colspan="7" height="40"><img class="formlogo"
                                         th:src="@{/resources/img/docicon.png}">
          <h3 class="formHeader"><b><u>REGISTER MANUAL</u></b></h3></td>
      </tr>

      <tr>
        <td>
          <label>Provider Name </label>
        </td>
        <td><label>:</label></td>
        <td>
          <input type="text" th:value="${providerId}" name="providerId" style="width:275px;"
                 readonly="readonly" hidden="hidden"> </input>
          <input type="text" th:value="${providerName}" name="providerName" style="width:275px;"
                 readonly="readonly"> </input>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td>
          <label>Member Card Number* </label>
        </td>
        <td><label>:</label></td>
        <td>
          <input type="text" name="cardNumber" th:field="*{cardNumber}"
                 onkeypress="return isNumberKey(event)"
                 style="width:190px; display: inline;"> </input>
          <input id="checkCardNum" class="default-button" type="submit" style="display: inline;"
                 value="Validasi">
        </td>
        <td width="500">
          <div class="error" style="display:inline;color:red;">
            <p id="cardNumError" style="margin-left:15px;" th:text="${cardNumError}"></p>
          </div>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td>
          <label>Member Name </label>
        </td>
        <td><label>:</label></td>
        <td>
          <input type="text" th:value="${memberName}" name="memberName" style="width:275px;"
                 readonly="readonly"> </input>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td>
          <label>Date Of Birth </label>
        </td>
        <td><label>:</label></td>
        <td>
          <input type="text" th:value="${birthday}" name="birthday" style="width:275px;"
                 readonly="readonly"> </input>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td><label>Client Name </label></td>
        <td><label>:</label></td>
        <td>
          <input type="text" th:value="${clientName}" name="clientName" style="width:275px;"
                 readonly="readonly"> </input>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td>
          <label>Admission Date </label>
        </td>
        <td><label>:</label></td>
        <td>
          <input id="datepicker" type="text"
                 style="margin-top:4px;width:230px;display:inline-block;"/>
          <span class="calendarbox"><img th:src="@{/resources/img/calendar.png}" height="25"
                                         width="35"></span>
        </td>
        <td class="sep"></td>
      </tr>

      <tr>
        <td><label>Claim Service* </label></td>
        <td><label>:</label></td>
        <td>
          <input type="text" th:value="${selectServiceIsEnabled}" name="selectServiceIsEnabled"
                 style="width:275px;" readonly="readonly" hidden="hidden"> </input>
          <select id="selectservice" th:field="*{caseCategoryList}" class="selectcss"
                  style="width:275px;margin-left: 5px;" disabled="disabled">
            <option th:each="case : ${caseCategoryList}" th:value="${case.caseCategoryId}"
                    th:text="${case.caseCategoryName + ' - ' + case.caseCategoryCode}"></option>
          </select>
        </td>
        <td width="500">
          <span class="error" style="display: inline;color:red;"><p id="select_error"></p></span>
        </td>
        <td class="sep"></td>
      </tr>

      <tr colspan="7" class="jumper"></tr>

      <tr>
        <td>
          <input id="addMember" class="remodal-confirm" type="submit" name="action"
                 value="Register"/>
          <input class="remodal-cancel" type="reset" value="Clear"/>
        </td>
      </tr>

    </table>
  </div>
</form>

单击“验证”按钮时,表单操作将转到弹出控制器,该控制器将卡号检查到数据库(checkCardNum),然后如果数据验证返回true,则表单将自动填充与该数据相关的数据。卡号,表格填写完数据后,我想用另一张卡号(不同于第一张经过验证的卡号)检查验证,但它给我一个错误400.

这是我的控制器

@PreAuthorize("hasAuthority('"+ACLConstant.MENU_REGISTRATIONMEMBERRKI+"')")
@RequestMapping(value="/member-rki-form",method={RequestMethod.GET , RequestMethod.POST} )
public String memberrkiform(Model model, HttpServletRequest  request, @ModelAttribute("formResult") MemberRkiForm formResult){


    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    UserAuth userAuth = (UserAuth)auth.getPrincipal();

    String[] cols = {"username", "deletedStatus"};
    Object[] values = {userAuth.getUserName(), 0};

    Users user = userService.findUserByColsAndValues(cols, values);

    int userid = user.getUserId();


    Users currentUser = userService.findUserByPK(userid);

    Provider provider = currentUser.getProvider();

    model.addAttribute("providerId", provider.getProviderId());
    model.addAttribute("providerName", currentUser.getFirstName()); 
    model.addAttribute("cardNumber", formResult.getcardNumber());
    model.addAttribute("cardNumError", formResult.getErrorMsg());
    model.addAttribute("memberName", formResult.getMemberName());
    model.addAttribute("birthday", formResult.getBirthday());
    model.addAttribute("clientName", formResult.getClientName());
    model.addAttribute("caseCategoryList", formResult.getCaseCategoryList());
    model.addAttribute("selectServiceIsEnabled", formResult.getSelectServiceIsEnabled());

    return "memberrki/member-rki-form";
}


@RequestMapping(value="/checkCardNum",method={RequestMethod.GET , RequestMethod.POST})
public String checkCardNum(@ModelAttribute("formResult") MemberRkiForm formResult, Model model, HttpServletRequest  request, RedirectAttributes redirectFormData){

    String errorMsg = "";

    String cardNumber = formResult.getcardNumber();

    if(cardNumber==null || cardNumber==""){

        errorMsg="Card Number Data Is Required";
    }
    else{

        String[] cols = {"currentCardNumber", "deletedStatus" , "subscriptionStatus"};
        Object[] values = {formResult.getcardNumber(), 0 , 1}; 
        Member member = memberService.findMemberByColsAndValues(cols, values);

        if(member==null){

            errorMsg="Member Not Found";
        }
        else{
            Member validatedMember = memberService.findMemberByPK(member.getMemberId());
            Client validatedMemberClient = validatedMember.getClient();

            formResult.setMemberName(validatedMember.getFirstName());
            formResult.setBirthday(validatedMember.getBirthday().toString());
            formResult.setClientName(validatedMemberClient.getClientName());

            MemberProductSearchParams memberProductParams = new MemberProductSearchParams();
            memberProductParams.setMember(validatedMember);
            memberProductParams.setDeletedStatus(0);
            memberProductParams.setSubscriptionStatus(1);

            Page<MemberProduct> memberProductPage  = memberService.findMemberProductListing(memberProductParams, null);
            List<MemberProduct> memberProductLists = memberProductPage.getContent();


            if(memberProductPage==null || memberProductLists==null){
                /*System.out.println("NO PRODUCT FOUND");*/
                errorMsg="NO PRODUCT AVAILABLE";
            }
            else{
                List<Product> productList = new ArrayList<Product>();
                List<CaseCategory> caseCategoryList = new ArrayList<CaseCategory>();

                for(int i=0; i<memberProductLists.size(); i++){
                String[] productCols = {"productId", "deletedStatus"};
                Object[] productValues = {memberProductLists.get(i).getProduct().getProductId(), 0}; 
                Product products = productService.findProductByColsAndValues(productCols, productValues);
                productList.add(products);

                String[] caseCategoryCols = {"caseCategoryId", "deletedStatus"};
                Object[] caseCategoryValues = {productList.get(i).getCaseCategory().getCaseCategoryId(), 0};
                CaseCategory caseCategories = caseCategoryService.findCaseCategoryByColsAndValues(caseCategoryCols, caseCategoryValues);
                caseCategoryList.add(caseCategories);

                }

                formResult.setCaseCategoryList(caseCategoryList);

            }


            formResult.setSelectServiceIsEnabled("true");
        }
    }

    formResult.setErrorMsg(errorMsg);
    formResult.setcardNumber(cardNumber);

    redirectFormData.addFlashAttribute("formResult", formResult);
    formResult = new MemberRkiForm();

    return "redirect:/memberrki/member-rki-form";
  }
}

也许有人可以帮我解决问题, Thx提前..(对不起我的英语不好)

0 个答案:

没有答案