2013-12-13 68 views
0

我有JSP,顯示在博客所有文章如何處理在Spring MVC的表單數據

<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 
+0

你得到這個時候,當你的請求頁面或當您發佈到控制器? –

+0

當請求頁/帖子 –

+0

你能告訴我們你的Post.java嗎? – Dropout

回答

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(); 
} 
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"; 
    } 

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

輸出:

Name: Test