2017-08-13 66 views
0

我使用百里香發佈表單到控制器。該表單的一個字段是一個名爲person的對象。在控制器中,我打印這個字符串,它看起來像一個包含人員中所有字段的地圖。現在,我只需要人的身份證,這樣我就可以獲得人物。我嘗試將該參數更改爲Person,但它表示無法將該字符串轉換爲該人員。我知道的唯一方法是使用正則表達式。但我認爲這有點愚蠢。有沒有簡單的方法來解決它?如何解碼使用百里香和彈簧形式提交的對象

這裏是HTML

<form action="#" class="form-horizontal center-block" 
       style="width: 40%" th:action="@{/index}" th:object="${project}" 
       method="post"> 
    <select th:field="*{person}"> 

        <option th:each="person:${persons}" th:value="${person}" 
         th:text="${person.name}">Wireframe</option> 
       </select> 
</form> 

起初,我有過這樣

@PostMapping("/index") 
      public String save(String projectName, Person person, String description, 
        @RequestParam(name="subject")String subject, String date) { 
       System.out.println(person);    
       return "redirect:/index"; 
      } 

控制器但我有一個錯誤說cant't字符串轉換爲個人

然後我改變了控制器像這樣

@PostMapping("/index") 
      public String save(String projectName, String person, String description, 
        @RequestParam(name="subject")String subject, String date) { 
       System.out.println(person);    
       return "redirect:/index"; 
      } 

在控制檯中,我得到了這個這個Person(id=1, name=mike, age=44, sex=女)。如果我想獲得這個對象,我需要使用正則表達式來分割字符串,然後我會得到Id。如果有一種簡單的方法可以輕鬆獲取人物對象?

+0

請發佈您的錯誤,並明確您的要求。 –

+0

沒有錯誤,我更新了要求 –

回答

0

我解決了它,改變了下面的html。

<form action="#" class="form-horizontal center-block" 
       style="width: 40%" th:action="@{/index}" th:object="${project}" 
       method="post"> 
    <select th:field="*{person}"> 

        <option th:each="person:${persons}" th:value="${person.id}" 
         th:text="${person.name}">Wireframe</option> 
       </select> 
</form>