[英]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.