springMvc 標籤 標籤須要注意的問題

  在用springMVC <form:form>表單時,喜歡報的錯誤以下所示:html

錯誤的Controller層的代碼以下:java

1 @RequestMapping(value = "test.do",method = RequestMethod.POST)
2     public String test(Student stu,Model model){
3        stu = new Student();
4         stu.setAge(16);
5         stu.setName("zyh");
6         stu.setId(39);
7 //        model.addAttribute("stu",stu);
8         return "springform";

 

其中BindingResult---對應於@ModelAttribute("什麼什麼")---->對應本例而言你應該修改成:@ModelAttribute("stu")這個stu是<form:form modelAttribute=''stu''>中的stu.web

r plain target object for bean name 'stu' available as request attribute對應於:model.addAttribute("stu",實體對象);
破解錯誤的只要二者有一個就能夠了,二者都有也是能夠的。

正確的Controller的代碼形式一以下加上model.addAttribute("stu",實體對象):
1 @RequestMapping(value = "test.do",method = RequestMethod.POST)
2     public String test(Student stu,Model model){
3        stu = new Student();
4         stu.setAge(16);
5         stu.setName("zyh");
6         stu.setId(39);
7         model.addAttribute("stu",stu);
8         return "springform";
9     }
正確的Controller的代碼形式二以下加上@ModelAttribute("stu")
1 @RequestMapping( value = "test.do",method = RequestMethod.POST)
2     public String test(@ModelAttribute("stu") Student stu,Model model){
3        stu = new Student();
4         stu.setAge(16);
5         stu.setName("zyh");
6         stu.setId(39);
7         return "springform";
8     }

正確的Controller的代碼形式三以下:即加上model.addAttribute("stu"),也加上@ModelAttribute("stu")spring

代碼以下:app

1 @RequestMapping( value = "test.do",method = RequestMethod.POST)
2     public String test(@ModelAttribute("stu") Student stu,Model model){
3        stu = new Student();
4         stu.setAge(16);
5         stu.setName("zyh");
6         stu.setId(39);
7         model.addAttribute("stu",stu);
8         return "springform";
9     }

其中跳到springform.jsp的代碼以下:dom

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: qinlinsen
 4   Date: 2017-07-18
 5   Time: 17:12
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
10 <%--使EL表達式生效的語句--%>
11 <%@ page isELIgnored="false" %>
12 <html>
13 <head>
14     <title>spring form</title>
15 </head>
16 <body>
17  <form:form action="/webtest/query.do" method="post" modelAttribute="stu">
18         <table>
19             <tr>
20                 <td>
21                     Name
22                 </td>
23                 <td>
24                     <form:input path="name"/>
25                 </td>
26             </tr>
27             <tr>
28                 <td>
29                     age
30                 </td>
31                 <td>
32                     <form:input path="age"/>
33                 </td>
34             </tr>
35             <tr>
36                 <td>
37                     id
38                 </td>
39                 <td>
40                     <form:input path="id"/>
41                 </td>
42             </tr>
43             <tr>
44                 <td colspan="2">
45                     <input type="submit" value="submit">
46                 </td>
47             </tr>
48 
49         </table>
50     </form:form>
51 hello spring form.
52 </body>
53 </html>

Student類的代碼以下:jsp

 1 package com.supwisdom.domain;
 2 
 3 /**
 4  * Created by qinlinsen on 2017-07-18.
 5  */
 6 public class Student {
 7     private String name;//學生姓名
 8     private Integer age;//學生年齡
 9     private Integer id;//學生省份證號
10 
11     public String getName() {
12         return name;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public Integer getAge() {
20         return age;
21     }
22 
23     public void setAge(Integer age) {
24         this.age = age;
25     }
26 
27     public Integer getId() {
28         return id;
29     }
30 
31     public void setId(Integer id) {
32         this.id = id;
33     }
34 }
相關文章
相關標籤/搜索