我是Spring的新手,我试图创建简单的表单示例,我有一个问题
当我点击提交并接收输入值并使其有效时,在控制器中:
@Valid Student student
我总是空值...我尝试更改映射,阅读帖子Spring MVC form input value is always null但没有任何帮助:(
我的控制器:
@Controller
@RequestMapping({"/rejestracja"})
public class RejestracjaController {
@Autowired
private StudentService studentService;
@RequestMapping(method=RequestMethod.GET)
public String rejestracjaSetup (Model model) {
model.addAttribute("student",new Student());
return "rejestracjaPage";
}
@RequestMapping(method=RequestMethod.POST)
public String dodajStuenta (@Valid Student student, BindingResult bindingResult) {
System.out.println("------");
System.out.print(student.getFirstname());
System.out.print("=====");
System.out.print(student.getLastname());
System.out.print("=====");
System.out.print(student.getYearLevel());
System.out.print("=====");
if (bindingResult.hasErrors()) {
return "rejestracjaPage";
}
return "redirect:/list";
}
}
我的.jsp:
<form:form method="POST" commandName="student">
<table>
<tr>
<td>First name</td>
<td>
<form:input path="firstname"/>
<form:errors path="firstname"/>
</td>
</tr>
<tr>
<td>Last name</td>
<td>
<form:input path="lastname"/>
<form:errors path="lastname"/>
</td>
</tr>
<tr>
<td>yearLevel</td>
<td>
<form:input path="yearLevel"/>
<form:errors path="yearLevel"/>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Register"></td>
</tr>
</table>
</form:form>
模特:
@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int studentId;
@Column
@NotNull
@Size(min = 6)
private String firstname;
@Column
@Length(min=3, max=50, message="Example message1")
private String lastname;
@Column
@Min(value = 1, message = "Example message2")
private int yearLevel;
public Student() {
}
....
getters and setters
....
}
我做错了什么?
答案 0 :(得分:0)
验证不是(还)您的问题。你的问题是空Student
bean。尝试将其与@ModelAttribute
绑定:
@RequestMapping(method=RequestMethod.POST)
public String dodajStuenta (
@Valid @ModelAttribute("student") Student student,
BindingResult bindingResult) {
}