【问题标题】:How to handle data from form in Spring MVC如何在 Spring MVC 中处理来自表单的数据
【发布时间】:2014-01-01 00:14:34
【问题描述】:

我有显示博客中所有帖子的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>

我有控制器来处理它

@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;
    }

}

现在我想添加表单来保存新帖子。

我在我的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>

我也添加到控制器。

@RequestMapping( value = "/posts/add", method = RequestMethod.POST)
    public String saveAdd(@ModelAttribute("post") Post post, ModelMap model) {

        model.addAttribute("posts", postDao.addPost(post));

        return "posts";
    }

域模型 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;
    }

}

但我明白了

 java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute

【问题讨论】:

  • 你什么时候得到这个,什么时候你请求页面或者你什么时候发布到控制器?
  • 当请求页面/posts
  • 你能给我们看看你的 Post.java 吗?

标签: java spring-mvc modelattribute spring-form


【解决方案1】:

因为您的 JSP 包含用于添加新帖子的新表单,所以当您转到 /posts 时,它需要模型属性 post

@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";
}

如果您发现必须在多个位置创建模型,您甚至可以将模型创建拆分为单独的方法。这将确保它始终可用。

@ModelAttribute("post")
public Post createModel() {
    return new Post();
}

【讨论】:

    【解决方案2】:

    来自控制器:

    @RequestMapping(value = "createcustomer",method = RequestMethod.GET)
        public String customer(Model model)
        {
            Customer cus=new Customer();
            cus.setCustomerNumber("Test");
            model.addAttribute("customer",cus);
            return "createcustomer";
        }
    

    在视图中:

    <%@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>
    

    输出:

    Name: Test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 2012-06-17
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 2015-10-16
      相关资源
      最近更新 更多