0

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

2 回答 2

0

因为您的 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";
}

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

@ModelAttribute("post")
public Post createModel() {
    return new Post();
}
于 2013-12-13T11:23:46.610 回答
0

从控制器:

@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
于 2014-04-11T10:22:00.760 回答