2017-02-16 64 views
0

我正在開發一個管理客戶機和應用程序的應用程序。 我已經創建了所有必要的表格,模式,控制器等。Spring MVC + Thymeleaf - 保存關係@ManyToOne

客戶實體具有列表和機器具有客戶關係(雙向,非可選)。

我所遇到的問題與向現有客戶端添加全新機器(提供此服務器已存在)有關。

因此,這裏是一些代碼短剪斷:

@Controller 
public class MachineController { 

... 
    @GetMapping("/machines/add/{clientId}") 
    public String addMachine(@PathVariable("clientId") int clientId, Model model) throws ClientNotFoundException { 
     model.addAttribute("machineTypes", MachineType.values()); 
     model.addAttribute("machine", new Machine()); 
     model.addAttribute("client", clientService.find(clientId)); 
     return "machines/form"; 
    } 
} 

    @PostMapping("/machines/save") 
    public String saveMachine(@ModelAttribute @Valid Machine machine, BindingResult bindingResult, Model model) 
      throws ClientNotFoundException { 

     model.addAttribute("machineTypes", MachineType.values()); 

     int clientId = machine.getClient().getId(); 
     LOG.debug("ClientId:{}", clientId); 
     // Client object is not filled here ! clientId is 0 (new client). 
} 

問題是與保存功能 - 我不知道怎麼打發exisitng客戶對象Machine對象,這是由HTTP POST發送。 我控制器抱怨說,客戶端不發送和BindingResult拋出錯誤:

Field error in object 'machine' on field 'client.address.city: rejected value [null]; Field error in object 'machine' on field 'client.address.zipCode: rejected value [null]; Field error in object 'machine' on field 'client.name': rejected value [null];

我loooking着任何幫助。

HTML形式介紹如下:

       <form class="form-horizontal" th:action="@{/machines/save}" th:object="${machine}" method="post" id="machineForm"> 
           <input type="hidden" th:field="*{id}"/> 

            <!-- Panel for machine --> 
           <div th:class="${#fields.hasErrors('machineType')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="manufacturer">Rodzaj:*</label> 
            <div class="col-sm-8"> 

             <select th:field="${machine.machineType}" class="form-control" id="machineTypeSelect"> 
              <option value="" disabled="disabled" selected="selected">Wybierz rodzaj</option> 
              <option th:each="type: ${machineTypes}" th:value="${type.name()}" th:text="${type}" th:attr="data-has-car=${type.hasCar()}"></option> 
             </select> 

             <div class="help-block" th:if="${#fields.hasErrors('machineType')}" 
              th:errors="*{machineType}"></div> 
            </div> 
           </div> 

           <div th:class="${#fields.hasErrors('manufacturer')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="manufacturer">Producent:*</label> 
            <div class="col-sm-8"> 
             <input type="text" 
               class="form-control" 
               id="manufacturer" 
               placeholder="Podaj producenta" 
               th:field="*{manufacturer}" /> 
             <div class="help-block" th:if="${#fields.hasErrors('manufacturer')}" 
              th:errors="*{manufacturer}"></div> 
            </div> 
           </div> 

           <div th:class="${#fields.hasErrors('model')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="model">Model:</label> 
            <div class="col-sm-8"> 
             <input type="text" 
               class="form-control" 
               id="model" 
               placeholder="Podaj model" 
               th:field="*{model}"/> 
             <div class="help-block" th:if="${#fields.hasErrors('model')}" 
              th:errors="*{model}"></div> 
            </div> 
           </div> 

           <div th:class="${#fields.hasErrors('productionYear')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="productionYear">Rok produkcji:*</label> 
            <div class="col-sm-8"> 
             <input type="number" 
               class="form-control" 
               id="productionYear" 
               placeholder="Podaj rok produkcji" 
               th:field="*{productionYear}"/> 
             <div class="help-block" th:if="${#fields.hasErrors('productionYear')}" 
              th:errors="*{productionYear}"></div> 
            </div> 
           </div> 

           <div th:class="${#fields.hasErrors('factoryNo')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="factoryNo">Numer fabryczny:</label> 
            <div class="col-sm-8"> 
              <input type="number" 
                class="form-control" 
                id="factoryNo" 
                placeholder="Podaj numer fabryczny" 
                th:field="*{factoryNo}"/> 
             <div class="help-block" th:if="${#fields.hasErrors('factoryNo')}" 
              th:errors="*{factoryNo}"></div> 
            </div> 
           </div> 

           <div th:class="${#fields.hasErrors('maxLoad')}? 'form-group has-error has-feedback' : 'form-group'"> 
            <label class="control-label col-sm-4" for="maxLoad">Max udżwig:</label> 
            <div class="col-sm-8"> 
             <div class="input-group"> 
             <input type="number" 
               class="form-control" 
               id="maxLoad" 
               placeholder="Max. udźwig" 
               aria-describedby="measure" 
               th:field="*{maxLoad}"/> 
              <span class="input-group-addon" id="measure">kg</span> 
             </div> 
             <div class="help-block" th:if="${#fields.hasErrors('maxLoad')}" 
              th:errors="*{maxLoad}"></div> 
            </div> 
           </div> 

           <div th:object="${machine.client}"> 
            <div th:class="${#fields.hasErrors('id')}? 'form-group has-error has-feedback' : 'form-group'"> 
             <label class="control-label col-sm-4" for="clientId">Wybrany klient:</label> 
             <div class="col-sm-8"> 
              <span class="form-control-static" id="selectedClient">Nie wybrano! Wyszukaj po prawej</span> 
              <input type="hidden" th:field="${machine.client.id}" id="clientId" /> 
              <div class="help-block" th:if="${#fields.hasErrors('id')}" 
               th:errors="${machine.client.id}"></div> 
             </div> 
            </div> 
           </div> 

           <div id="machineCar" th:object="${machine.car}"> 

            <div th:class="${#fields.hasErrors('make')}? 'form-group has-error has-feedback' : 'form-group'"> 
             <label class="control-label col-sm-4" for="carMake">Marka pojazdu:*</label> 
             <div class="col-sm-8"> 
              <input type="text" 
                class="form-control" 
                id="carMake" 
                placeholder="Podaj markę pojazdu" 
                th:field="*{make}"/> 
              <div class="help-block" th:if="${#fields.hasErrors('make')}" 
               th:errors="*{make}"></div> 
             </div> 
            </div> 

            <div th:class="${#fields.hasErrors('vin')}? 'form-group has-error has-feedback' : 'form-group'"> 
             <label class="control-label col-sm-4" for="factoryNo">VIN:</label> 
             <div class="col-sm-8"> 
              <input type="number" 
                class="form-control" 
                id="vin" 
                placeholder="Podaj numer VIN" 
                th:field="*{vin}"/> 
              <div class="help-block" th:if="${#fields.hasErrors('vin')}" 
               th:errors="*{vin}"></div> 
             </div> 
            </div> 
           </div> 
            <div class="form-group"> 
             <div class="col-xs-12"> 
              <button type="submit" class="btn btn-primary">Zapisz dane</button> 
             </div> 
            </div> 
           </form> 

回答

0

我解決了我的問題。不知道這是否是正確的解決方案,但工作。

因此,只需在機器實體:

public class Machine { 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MACHINE_SEQUENCE") 
    private int id; 

    // ... 
    @ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE}) 
    @JoinColumn(name = "client_id", nullable = false) 
    @Valid // REMOVED 
    private Client client; 

我已經取消了對客戶端領域的頂部,因此Spring MVC的不確認Thymeleaf形式@Valid註解了。

另一個解決辦法是包括與客戶的詳細資料隱藏的輸入(姓名,公司和子地址對象),所以thymeleaf可以傳輸完整的對象和BindingResult不會抱怨...

0

嘗試在HTML表單中添加

<input type="hidden" name="client.id" value="${client.id}" /> 

,與ID的客戶對象值將被創建,然後離開,其餘的存儲庫,它只需要id來關聯記錄。

+0

我已經做以前,這些隱藏的輸入,但仍不起作用。 – mlewandowski

+0

你介意分享你的HTML表單嗎? –

+0

我在上面的問題描述中發佈了我的html表單。 – mlewandowski

相關問題