[英]How to handle data from form in Spring MVC
I have jsp that show all posts in blog 我有jsp可以显示博客中的所有帖子
<body>
<table>
<c:forEach var="post" items="${posts}">
<tr>
<td>${post.id}</td>
<td>${post.text}</td>
<td><a href="authors?name=${post.author}">Show author</a></td>
</tr>
</c:forEach>
</table>
<div>
<a href="posts/get.json">JSON</a> <a href="posts/get.xml">XML</a>
</div>
</body>
I have controller to handle it 我有控制器来处理
@Controller
public class PostsController {
@Autowired
private PostDAO postDao;
@RequestMapping("/posts")
public String showAllPosts(ModelMap model) {
List<Post> posts = postDao.findAll();
model.addAttribute("posts", posts);
return "posts";
}
@RequestMapping("/posts/get")
public List<Post> getAllPosts() {
List<Post> posts = postDao.findAll();
return posts;
}
}
Now I want to add form to save new post. 现在,我想添加表单以保存新帖子。
I add form in my jsp 我在我的jsp中添加表格
<form:form method="POST" action="/posts/add" modelAttribute="post">
<table>
<tr>
<td><form:label path="id">Id:</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="text">Text:</form:label></td>
<td><form:input path="text" /></td>
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
Also I add to controller. 我也添加到控制器。
@RequestMapping( value = "/posts/add", method = RequestMethod.POST)
public String saveAdd(@ModelAttribute("post") Post post, ModelMap model) {
model.addAttribute("posts", postDao.addPost(post));
return "posts";
}
Domain model Post.java 域模型Post.java
public class Post {
private int id;
private String author;
private String text;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
But I get 但是我明白了
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute
Because your JSP contains the new form for adding a new post, it will need the model attribute post
to be present when you go to /posts
. 因为你的JSP包含用于添加一个新的岗位,新的形式,它需要的模型属性post
到场,当你去/posts
。
@RequestMapping("/posts")
public String showAllPosts(ModelMap model) {
List<Post> posts = postDao.findAll();
model.addAttribute("post", new Post()); // Add empty form backing object
model.addAttribute("posts", posts);
return "posts";
}
You could even split out model creation to a separate method if you find you're having to create the model in multiple places. 如果发现必须在多个位置创建模型,甚至可以将模型创建拆分为单独的方法。 This will ensure that its always available. 这将确保其始终可用。
@ModelAttribute("post")
public Post createModel() {
return new Post();
}
From Controller: 从控制器:
@RequestMapping(value = "createcustomer",method = RequestMethod.GET)
public String customer(Model model)
{
Customer cus=new Customer();
cus.setCustomerNumber("Test");
model.addAttribute("customer",cus);
return "createcustomer";
}
In View: 在视图中:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<div class="cl">
<form:form commandName="customer" method="POST">
<p>Name: <c:out value="${customer.CustomerNumber}"></c:out></p>
</form:form>
<div>
Output: 输出:
Name: Test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.