Reputation: 5543
Can you tell me how to pass several parameters from the view to the controller's function in the most convenient way?
JSP-view:
<h2>${topic.getName()}</h2>
<h3>${topic.getText()}</h2>
<form:form method="post" commandName="newComment">
<fieldset>
<div class="editor-label">
<td><form:label path="text">Input comment</form:label></td>
</div>
<div class="textarea">
<form:textarea path="text" />
</div>
<p>
<input type="submit" value="Comment" />
</p>
</fieldset>
</form:form>
As you can see, we have topic and newComent properties, which represent topic and comment entities.
Here is a controller:
@RequestMapping(value = "/addComment/{topicId}", method = RequestMethod.POST)
public String saveComment(@ModelAttribute("newComment")Comment comment, BindingResult result, Model model){
validate(comment, result);
if (result.hasErrors() )
{
return "//";
}
return "redirect:details/";
}
}
the comment entity is recognized fine but i need an instance of Topic object (or it's ID) as well. An instance of topic object was accessible in the view, and topic ID is a part of a response. Can you give me an idea how can I deal with this problem?
Upvotes: 1
Views: 289
Reputation: 2443
Can you get the topic model if you have the topicId
? It's in your path.
You can get it by adding @PathVariable
annotation in your method parameters.
public String saveComment(@PathVariable String topicId, @ModelAttribute("newComment")Comment comment, BindingResult result, Model model){
Upvotes: 1