I’m creating a simple training project. I’ve implemented a controller method, which deletes an item from the list. The method is looking like this:
@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:";
}
}
This method is called from the button-tag, which is looking in the following way:
<form><button formaction = "../deleteComment/1" formmethod = "post">delete</button></form>
In my project the form-tag is looking like a clickable button. But there is a serious problem: controller’s method is never triggered. How can I trigger it, using a button-tag?
P.S. the call is performed from the page with URI http://localhost:8080/simpleblog/topic/details/2 and controller’s URI is the http://localhost:8080/simpleblog/topic/deleteComment/2
UPDATE:
I’ve created hyperlink ‘delete’, clicking on which should delete a comment, and I received an exception
java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!
And it’s true: I really have no @ModelAttribute in my controller method, preceding BindingResult parameter. But I have no clue, which type of type should it be?
<form>‘smethodattribute isGETby default. I don’t know what are you trying to do withformmethodandformactionattributes, but in default HTML they mean nothing.You must try something like:
EDIT:
You are declaring some unused parameters in your method. BindingResult must be used with a
@Validannotated attribute (search@Validhere to see some examples), but this is not your case. So, please just try: