簡単なトレーニングプロジェクトを作成しています。リストからアイテムを削除するコントローラーメソッドを実装しました。メソッドは次のようになります。
@Controller
@RequestMapping(value = "/topic")
public class TopicController {
@Autowired
private TopicService service;
...
@RequestMapping(value = "/deleteComment/{commentId}", method = RequestMethod.POST)
public String deleteComment(@PathVariable int commentId, BindingResult result, Model model){
Comment deletedComment = commentService.findCommentByID(commentId);
if (deletedComment != null) {
commentService.deleteComment(deletedComment);
}
return "refresh:";
}
}
このメソッドは、次のように表示されるボタンタグから呼び出されます。
<form><button formaction = "../deleteComment/1" formmethod = "post">delete</button></form>
私のプロジェクトでは、フォームタグはクリック可能なボタンのように見えます。しかし、深刻な問題があります。コントローラーのメソッドがトリガーされることはありません。ボタンタグを使用してトリガーするにはどうすればよいですか?
PS呼び出しは、URI http:// localhost:8080 / simpleblog / topic / details / 2のページから実行され、コントローラーのURIはhttp:// localhost:8080 / simpleblog / topic / deleteComment/2です。
アップデート:
コメントを削除するハイパーリンク「delete」を作成しましたが、これをクリックすると例外が発生しました
java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!
そして、それは真実です。BindingResultパラメーターの前に、コントローラーメソッドに@ModelAttributeがありません。しかし、私には手がかりがありません、それはどのタイプのタイプである必要がありますか?