2017-11-06 10 views
0

まず、データベースからテーブルに値を渡します。最初の列では、コントローラのIDを削除する関数を渡すフォームを作成したいと思います。コントローラにIDを渡す

<tr th:each="blg: ${all}" th:object="${blg}" > 
    <td> 
    <form th:action="@{/delete}" th:object= "${blg}" method="post"> 
    <input type="text" th:field="${blg.id}"/> 
    <button type="submit">Delete</button> 
    </form> 
    </td> 
    <td th:text="*{title}"> title </td>  
    <td th:text="*{content}"> title </td> 
    <td th:text="*{category}"> title </td> 
    <td th:text="*{signature}"> title </td> 
</tr> 

コントローラー:

@GetMapping("/show") 
public String show(Model model){ 
    List<Blog> all = br.findAll(); 
    model.addAttribute("all",all); 
    return "show"; 
} 

@RequestMapping(value="/delete", method=RequestMethod.POST) 
    public String deletePost(@RequestParam Long id){   
     br.delete(id);   
     return "redirect:/show"; 
    } 

thymeleafエンジンは、このエラーocccursとしてオブジェクトをマップしません:

java.lang.IllegalStateException:BindingResultもプレーンなターゲットオブジェクトどちらBean名は 'リクエスト属性として利用可能な「blg」。

この場合、正しいフォームを作成する方法は何ですか。

答えて

0

下図のようにHTMLコードを更新します。

<tr th:each="blg: ${all}" > 
    <td> 
     <form th:action="@{|/delete/${blg.id}|}" method="post"> 
      <button type="submit">Delete</button> 
     </form> 
    </td> 
    <td th:text="${blg.title}"> title </td>  
    <td th:text="${blg.content}"> title </td> 
    <td th:text="${blg.category}"> title </td> 
    <td th:text="${blg.signature}"> title </td> 
</tr> 

削除操作のためのHTTPメソッドDELETEを使用することをお勧め。

th:objectは、属性blgのリクエスト属性を調べようとしたためです。しかし、blgは反復の結果です。

関連する問題