Когда я пытаюсь обновить запись, она дублируется и вызывает функцию добавления новой записи. - PullRequest
0 голосов
/ 26 октября 2018

Это мой контроллер для обновления новой кредитной карты, я поставил скрытый ввод, чтобы дать мне идентификатор, и я найду его по идентификатору

@RequestMapping("/updateCreditCard") 
public String UpdateCreditCard(@RequestParam("id") Long id, Model model,Principal principal) {
      users user = userService.findByUsername(principal.getName());
      UserPayment userPayment = userPaymentService.findById(id).get();
      UserBilling userBilling = userPayment.getUserBilling();
      if (userPayment.getId() == userBilling.getUserPayment().getId()) {
        model.addAttribute("user", user);
        model.addAttribute("userPayment", userPayment);
        model.addAttribute("userBilling", userBilling);
        model.addAttribute("userPaymentList", user.getUserPaymentList());
        List <String> stateList = EGConstant.ListOfStatesNames;
        Collections.sort(stateList);
        model.addAttribute("stateList", stateList);
        model.addAttribute("addNewCreditsCards",  true);
        model.addAttribute("classActiveBilling", true);
     } else {
        return "redirect:/error";
       }
    return "myProfile";
   }

Это метод для добавленияNewCreditcards в базу данных, когда япопробуйте обновить кредитную карту, он снова вызывает, но не обновляет, но делает новую запись

 RequestMapping(value = "/addNewCreditsCards", method = RequestMethod.POST) 
 public String addNewCredidCard(@ModelAttribute("userPayment") UserPayment userPayment,
                                @ModelAttribute("userBilling") UserBilling userBilling,
                                Model model,Principal principal) 
 {
    users user = userService.findByUsername(principal.getName());
    userService.saveUserBillingAndPayment(userBilling, userPayment, user);
    model.addAttribute("user", user);
    model.addAttribute("userPaymentList", user.getUserPaymentList());
    model.addAttribute("classActiveBilling", true);
    model.addAttribute("listOfCreditsCards", true);
    return "myProfile";
 }

И это мой шаблон, он разделен на вкладки, и я использую класс, чтобы добавить, чтобы появилось выбранноевкладка

Billing  tab 
<div class="tab-pane fade" id="tab-4"
   th:classappend="${classActiveBilling}? 'in active'">
<div class="panel-group">
<div class="panel panel-default" style="border: none;">
<div class="panel-body"
   style="background-color: #ededed; margin- 
   top: 20px;">
<ol class="breadcrumb">
   <li class="breadcrumb-item active"><a
      th:href="@{/listOfCreditsCards}"
      th:style="${listOfCreditsCards}? 'color:red'">List Of
      Credit Card</a>
   </li>
   <li class="breadcrumb-item active"><a
      th:href="@{/addNewCreditsCards}"
      th:style="${addNewCreditsCards}? 'color:red'">Add new
      Credit Card</a>
   </li>
</ol>
<div th:if="${listOfCreditsCards}">
   <form th:action="@{/setDefaultPayment}" method="post">
      <table class="table">
         <thead>
            <tr>
               <td>Default</td>
               <td>Credit Card</td>
               <td>Operations</td>
            </tr>
         </thead>
         <tbody>
            <tr th:each="userPayment : ${userPaymentList}">
               <input type="hidden" name="id" th:value="${userPayment.id}" />
               <td><input type="radio" name="defaultUserPaymentId"
                  th:value="${userPayment.id}"
                  th:checked="${userPayment.defaultPayment}" /></td>
               <td th:text="${#strings.toUpperCase(userPayment.type)}"></td>
               <td><a style="text-decoration:none"
                  th:href="@{/updateCreditCard?id=}+${userPayment.id}">
                  <i class="fas fa-pencil-alt"></i> Update&nbsp; &nbsp; &nbsp;
                  </a> <a style="text-decoration:none" th:href="@{/removeCreditCard?id=}+${userPayment.id}">
                  <i class="fas fa-times"></i> Delete
                  </a>
               </td>
            </tr>
         </tbody>
      </table>
      <button class="btn btn-prmary" type="submit">Save</button>
   </form>
</div>
<div th:if="${addNewCreditsCards}" >
   <form th:action="@{/addNewCreditsCards}" method="POST">
      <div class="bg-info" th:if="${updateUserPaymentInfo}">
         User info updated
      </div>
      <input type="hidden" name="id" th:value="${userPayment.id}" />  
      <div class="form-group">
         <h5>* Give a name to your card</h5>
         <input class="form-control" type="text" th:name="cardName" placeholder="Enter credit card name"
            th:value="${userPayment.cardName}" required="required" />
      </div>
      <!--  Biiling Addresse -->
      <div class="form-group">
         <label for="billingName">* Name</label> <input type="text"
            id="billingName" class="form-control" 
            th:name="userBillingName" placeholder="Reciver Name" required="required"
            th:value="${userBilling.userBillingName}" />
      </div>
      <div class="form-group">
         <label for="billingAddresse">* Street Name</label> <input
            type="text" id="userBillingStreet1" class="form-control"
            th:name="userBillingStreet1" placeholder="Street Name 1" required="required"
            th:value="${userBilling.userBillingStreet1}" /> 
         <br />
         <input type="text" id="userBillingStreet2" class="form-control"
            th:name="userBillingStreet2" placeholder="Street Number"
            th:value="${userBilling.userBillingStreet1}"  />
      </div>
      <div class="row">
         <div class="col-xs-4">
            <div class="form-group">
               <label for="userBillingCity">* City</label> <input type="text "
                  id="userBillingCity" class="form-control"
                  th:name="userBillingCity" placeholder="Enter your city" required="required"
                  th:value="${userBilling.userBillingCity}" />
            </div>
         </div>
         <div class="col-xs-4">
            <label for="userBillingState">* State</label> 
            <select th:name="userBillingState" class="form-control" 
               th:value="${userBilling.userBillingState}"  required="required">
               <option disabled="disabled" selected="selected" >select a state</option>
               <option th:each="state : ${stateList}" th:text="${state}" 
                  th:selected="(${userBilling.userBillingState} == ${state})"
                  ></option>
            </select>
         </div>
         <div class="col-xs-4">
            <div class="form-group">
               <label for="ZipCode">* zipCode</label> <input type="number "
                  id="userBillingZipCode" class="form-control"
                  th:name="userBillingZipCode" placeholder="Enter your zip code" required="required"
                  th:value="${userBilling.userBillingZipCode}" />
            </div>
         </div>
      </div>
      <!--  CreditCard Information -->
      <hr>
      <div class="form-group">
         <h5>Credit card information</h5>
      </div>
      <div  class="row">
         <div class="col-xs-12">
            <img class="img-responsive" src="/image/creditcard.png" /><br/>
            <div class="form-group">
               <label for="cardType">* Select Card Type</label>
               <select class="form-control" th:value="${userPayment.type}" id="type" th:name="type">
                  <option selected="selected" disabled="disabled" >Choose your credit card type</option>
                  <option value="visa" >Visa</option>
                  <option value="masterCard" >Master Card</option>
                  <option value="discover" th:value="${userPayment.type}">Discover</option>
                  <option value="CIB" >CIB</option>
               </select>
            </div>
            <div class="form-group">
               <label for="holderName" > * Holder Name</label>
               <input class="form-control" type="text" th:name="holderName" id="holderName" th:value="${userPayment.holderName}"
                  required="required " placeholder="Enter Card Holder Name" autocomplete="off" />
            </div>
            <div class="form-group">
               <label for="cardNumber" > * Card Number</label>
               <div class="input-group">
                  <input class="form-control" type="tel" th:name="cardNumber" id="cardNumber" th:value="${userPayment.cardNumber}"
                     required="required " placeholder="Enter Card Number " autocomplete="off" />
                  <span class="input-group-addon"> <i class="fa fa-credit-card"></i> </span>
               </div>
            </div>
         </div>
      </div>
      <div class="row">
         <div class="col-xs-7">
            <div class="form-group">
               <label for="Expiration Date" style="margin-left:175px"> * Expiration Date</label>
               <div class="row">
                  <div class="col-xs-6">
                     <select class="form-control" required="required" name="expiryMonth" id="expiryMonth" th:value="${userPayment.expiryMonth}">
                        <option disabled >-- Month --</option>
                        <option value="01" >Jan (01)</option>
                        <option value="02" >Feb (02)</option>
                        <option value="03" >Mars (03)</option>
                        <option value="04" >April (04)</option>
                        <option value="05" >May (05)</option>
                        <option value="06" >June (06)</option>
                        <option value="07" >July (07)</option>
                        <option value="08" >Aug (08)</option>
                        <option value="09" >Sept (09)</option>
                        <option value="10" >Oct (10)</option>
                        <option value="11" >Nov (11)</option>
                        <option value="12" >Dec (12)</option>
                     </select>
                  </div>
                  <div class="col-xs-6">
                     <select class="form-control" required="required" name="expiryYear" id="expiryYear" th:value="${userPayment.expiryYear}">
                        <option disabled="disabled">-- Year --</option>
                        <option value="2019">2019</option>
                        <option value="2019">2020</option>
                        <option value="2019">2021</option>
                        <option value="2019">2022</option>
                        <option value="2019">2023</option>
                        <option value="2019">2024</option>
                        <option value="2019">2025</option>
                        <option value="2019">2026</option>
                     </select>
                  </div>
               </div>
            </div>
         </div>
         <div class="col-xs-5">
            <div class="form-group">
               <label for="cvc">*Cvs Code</label>
               <input type="tel" class="form-control" id="cvc" name="cvc" th:value="${userPayment.cvc}" 
                  placeholder="Enter Cvc Code" required="required"/>
            </div>
         </div>
      </div>
      <hr />
      <button type="submit" class="btn btn-primary">Save All</button>
   </form>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...