springMvc <form:form>标签 <form:input>标签需要注意的问题

  在用springMVC <form:form>表单时,喜欢报的错误如下所示:

错误的Controller层的代码如下:

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.

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")

代码如下:

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的代码如下:

 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类的代码如下:

 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 }



原文地址:https://www.cnblogs.com/1540340840qls/p/7205281.html