无法将表单变量绑定到 Spring MVC 中的 ModelAttribute 注释变量
Not able to bind form variables to ModelAttribute annotated variable in Spring MVC
我有一个表格:
<form action="modelattributebinding" method="post">
<input name = "student1.firstName" value = "Michael">
<input name = "student1.lastName" value="Jackson" >
<input name="student1.age" value="34">
<input name="student1.hobby" value="music">
<input type="submit">
</form>
以及控制器中表单发布到
的方法
@RequestMapping(value = "/modelattributebinding" ,
method = RequestMethod.POST)
public ModelAndView binding(@ModelAttribute("student1")Student1 student1){
println student1.getFirstName()
null
}
我也有一个这样的class Student1
public class Student1{
String firstName
String lastName
int age
String hobby
@Override
public String toString() {
"firstName $firstName , lastName $lastName"
}
代码写在Groovy
我的理解是提交表单时springmvc会根据表单中的值创建一个Student1对象,然后将表单中的值绑定到Student1对象的属性
示例无效。我的理解有什么不正确的地方?或者我在上面的例子中犯了什么错误?
错误是使用 student1.firstName 而不是 firstName
如果我将表单更改为如下所示,则 ModelAttribute 注释会正确绑定
<form action="modelattributebinding" method="post">
<input name = "firstName" value = "Michael">
<input name = "lastName" value="Jackson" >
<input name="age" value="34">
<input name="hobby" value="music">
<input type="submit">
</form>
我有一个表格:
<form action="modelattributebinding" method="post">
<input name = "student1.firstName" value = "Michael">
<input name = "student1.lastName" value="Jackson" >
<input name="student1.age" value="34">
<input name="student1.hobby" value="music">
<input type="submit">
</form>
以及控制器中表单发布到
的方法@RequestMapping(value = "/modelattributebinding" ,
method = RequestMethod.POST)
public ModelAndView binding(@ModelAttribute("student1")Student1 student1){
println student1.getFirstName()
null
}
我也有一个这样的class Student1
public class Student1{
String firstName
String lastName
int age
String hobby
@Override
public String toString() {
"firstName $firstName , lastName $lastName"
}
代码写在Groovy
我的理解是提交表单时springmvc会根据表单中的值创建一个Student1对象,然后将表单中的值绑定到Student1对象的属性
示例无效。我的理解有什么不正确的地方?或者我在上面的例子中犯了什么错误?
错误是使用 student1.firstName 而不是 firstName
如果我将表单更改为如下所示,则 ModelAttribute 注释会正确绑定
<form action="modelattributebinding" method="post">
<input name = "firstName" value = "Michael">
<input name = "lastName" value="Jackson" >
<input name="age" value="34">
<input name="hobby" value="music">
<input type="submit">
</form>