[英]How can I send form values from jsp file to controller in Spring, JSP?
我正在尝试设计一个允许用户对彼此的个人资料进行评论的网页。 为此,登录用户单击另一个用户的名称,然后查看其详细的个人资料页面,并可以在该页面上发表评论。 因此, Comment表具有两个外键列,即commentator_id和owner_id 。 当用户在另一个用户的个人资料上发表评论时,评论表将保留两个用户的ID和文本,评论标题等。
这是我的控制器 :
@RequestMapping(value = "/comment", method = RequestMethod.GET)
public ModelAndView comment(Principal principal, String username, @ModelAttribute Comment c) {
ModelAndView result = new ModelAndView("result");
User user = userService.findByUsername(username); // the user who gets the comment
String username2 = (String) ((Authentication) principal).getPrincipal(); //logged in user who makes the comment
User authenticatedUser = userService.findByUsername(username2);
c.setCommentator(authenticatedUser);;
c.setOwner(user);
user.getComments().add(c);
userService.save(host);
String message = "Com was successfully added.";
result.addObject("message", message);
return result;
}
我的jsp文件:
<body>
<form method="post" action="comment">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Leave a referance</th>
</tr>
</thead>
<tbody>
<tr>
<td>Title</td>
<td><input type="text" name="title" value="" /></td>
</tr>
<tr>
<td>Text</td>
<td><input type="text" name="message" value="" /></td>
</tr>
// ...
</tbody>
</table>
</center>
<a href="<c:url value="/comment"><c:param name="username" value="${user.getUsername()}"/></c:url>">Send</a>
</form>
</body>
通过这种方式,用户可以在另一个用户的个人资料上进行评论,但是Comment表仅保留两个用户的ID,而不能保留我从表单中获得的文本,标题和其他值。 如何将表单值保留在数据库中?
使用ModelAttribute批注
public ModelAndView getMessage(@ModelAttribute("s") Student s){
ModelAndView model=new ModelAndView("Successpage");
return model;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.