0

簡単なトレーニングプロジェクトを作成しています。リストからアイテムを削除するコントローラーメソッドを実装しました。メソッドは次のようになります。

@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がありません。しかし、私には手がかりがありません、それはどのタイプのタイプである必要がありますか?

4

1 に答える 1

2

<form>method属性はGETデフォルトです。何をしようとしているのかformmethodformaction属性はわかりませんが、デフォルトのHTMLでは何の意味もありません。

次のようなことを試してください。

<form action="../deleteComment/1" method="post">
    <button>delete</button>
</form>

編集:

メソッドでいくつかの未使用のパラメーターを宣言しています。BindingResultは、注釈付きの属性とともに使用する必要があります(いくつかの例を確認するには、ここ@Validを検索してください)が、そうではありません。だから、試してみてください:@Valid

@RequestMapping(value = "/deleteComment/{commentId}", method = RequestMethod.POST)
public String deleteComment(@PathVariable int commentId){
    ...
}
于 2012-09-03T21:36:41.490 に答える