2017-02-26 188 views
0

我試圖創建我的第一個CRUD。 這裏是我的journey.html表代碼。將對象的變量傳遞給thymeleaf中的控制器

<table> 
    <tr th:each="trip : ${trips}"> 
     <td th:text="${trip.title}"></td> 
     <td th:text="${trip.destination}"></td> 
     <td th:text="${trip.id}"></td> 
      <form th:action="@{/journeys}" th:object="${trip}" method="post"> 
       <input type="hidden" th:field="${trip.id}" /> 
       <button type="submit">Delete</button> 
      </form> 
    </tr> 
</table> 

並使我的控​​制器看起來像現在這樣。

@RequestMapping(value = {"/journeys"}, method = RequestMethod.GET) 
public String journeysPage(Model model){ 
    tripRepository.save(new Trip("Asian Trip", "Asia")); 
    tripRepository.save(new Trip("European Trip", "Europe")); 
    tripRepository.save(new Trip("African Trip", "Africa")); 

    model.addAttribute("trips", tripRepository.findAll()); 
    return "journeysSite"; 
} 

@RequestMapping(value = {"/journeys"}, method = RequestMethod.POST) 
public String journeysPageTripDeleting(@RequestParam Long id) { 
    tripRepository.delete(id); 
    return "journeysSite"; 
} 

我要的是顯示在表中/我的行程我所有的行程。在每一行中都會有一個刪除按鈕,它會POST trip.id,將它從數據庫中刪除並重定向到完全相同的頁面,但刪除了行程。

但很明顯出現了錯誤:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'id' available as request attribute at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]

會有人給我一個提示怎麼辦呢?謝謝。

回答

0

您需要將您的控制器方法郵政編碼從@RequestMapping(value = {"/journeys/"}, method = RequestMethod.POST)更改爲@RequestMapping(value = {"/journeys/{id}"}, method = RequestMethod.POST)

正如你所看到的,你忘了在需要的RequestMapping中添加{id}

也是其標準使用DELETE http方法刪除實體不是POST方法。

+0

謝謝回答。仍然:在Chrome中執行處理器'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'(journeysSite:18)時出錯。在INTELLIJ中,我有java.lang.IllegalStateException:既沒有BindingResult也沒有bean名稱'id'的普通目標對象可用作請求屬性 –

+0

@ Karol.T您是否構建了代碼並重試? –

+0

當然,我:)我不知道如果我的HTML代碼中沒有錯誤。 –

0

在你的表格中,你定義了一個th:object="${trip}"對象,這意味着每當這個表單被提交時,這個trip對象將作爲請求主體發送。因此,要接收這個對象,你必須在控制器的方法中接受它。

@RequestMapping(value = {"/journeys/"}, method = RequestMethod.POST) 
public String journeysPageTripDeleting(@ModelAttribute Trip trip){ 
    tripRepository.delete(trip.getId()); 

    return "redirect:/journeys"; 
} 

th:field="${id}"將包含在該模型屬性提供這樣的對象,trip對象將有你要找的ID。

更多關於this

UPDATE: 根據您當前的控制器的方法實現,我認爲,所有你需要改變的就是這個,

<input type="hidden" th:field="*{id}" /> // No trip.id

+0

感謝您的回答。非常有用,現在我知道它是如何工作的。事情是我仍然得到錯誤。也許我的錯誤是在控制器負責獲取?它發生在我請求/旅程 –

+0

錯誤現在說什麼? – fluffyBatman

+0

我剛剛重組了這個問題。如果你現在可以看看它,我將不勝感激 –

0

這是不好的做法爲每一行創建表單。相反,這樣來做:

<table> 
    <tr th:each="trip : ${trips}"> 
     <td th:text="${trip.title}"></td> 
     <td th:text="${trip.destination}"></td> 
     <td th:text="${trip.id}"></td> 
     <td><button class='delete' data-id="${trip.id}">Delete</button></td> 
    </tr> 
</table> 

加入這個js在你的HTML:

<script> 
    $(document).ready(function() { 
     $(document).on('click', '.delete', function() { 
      $.ajax({ 
       url: "<c:url value="/journeys/delete"/>", 
       data: { 
        id: $(this).data("id") 
       }, 
       success: function(data) { 
        location.reload(); 
       } 
      }) 
     }) 
    }) 
</script> 

,改變你的控制器的方法是這樣的:

@RequestMapping(value = {"/journeys/delete"}, method = RequestMethod.GET) 
@ResponseBody 
public String journeysPageTripDeleting(@RequestParam Long id) { 
    tripRepository.delete(id); 
    return "success"; 
} 
+0

您甚至可以在JavaScript代碼中添加刪除確認。 – mhshimul

相關問題