Java 带ModelAndView的Spring MVC验证
我正在尝试将验证添加到我的SpringMVC应用程序中。在尝试设置验证之前,我一直在使用ModelAndView来提供jsp页面,但没有显示错误消息 模型 控制器Java 带ModelAndView的Spring MVC验证,java,spring,validation,spring-mvc,Java,Spring,Validation,Spring Mvc,我正在尝试将验证添加到我的SpringMVC应用程序中。在尝试设置验证之前,我一直在使用ModelAndView来提供jsp页面,但没有显示错误消息 模型 控制器 @RestController @RequestMapping("/employee") public class EmployeeController { private static final Logger LOGGER = LogManager .getLogger(EmployeeController.cl
@RestController
@RequestMapping("/employee")
public class EmployeeController {
private static final Logger LOGGER = LogManager
.getLogger(EmployeeController.class.getName());
@Autowired
private EmployeeServiceImpl employeeService;
@RequestMapping(value = "/new", method = RequestMethod.GET)
public ModelAndView getRegisterView(Model m) {
return new ModelAndView("addEmployee", "employeeForm", new Employee());
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView postUser(@Valid Employee employee, BindingResult result) {
ModelAndView model;
if (result.hasErrors()) {
model = new ModelAndView("addEmployee", "employeeForm", employee);
} else {
employeeService.addEmployee(employee);
model = new ModelAndView();
model.setViewName("displayEmployee");
model.addObject("employees", employeeService.getEmployees());
}
return model;
}
}
形式
名字:
姓氏:
电邮:
电话号码:
密码:
登记
清楚的
您自己在代码中破坏了模型,因此当返回页面时,基本上什么都没有了。当返回ModelAndView
Spring时,MVC假设您已经准备并添加了自己渲染视图所需的所有内容
而是将@modeldattribute(“employeeForm”)
添加到@Valid
注释旁边的方法参数中,并使用BindingResult
中已经存在的模型来构建模型和视图
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView postUser(@Valid @ModelAttribute("employeeForm" Employee employee, BindingResult result) {
ModelAndView model;
if (result.hasErrors()) {
model = new ModelAndView("addEmployee", result.getModel());
虽然这会起作用,但为什么不简单地返回一个字符串作为视图的名称,并在需要时进行一些模型的准备工作呢
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String postUser(@Valid @ModelAttribute("employeeForm") Employee employee, BindingResult result, Model model) {
if (result.hasErrors()) {
return "addEmployee";
} else {
employeeService.addEmployee(employee);
model.addObject("employees", employeeService.getEmployees());
return "displayEmployee";
}
}
我甚至认为,为displayEmployee
页面填充Model
不属于这里,而是用一种单独的方法为该页面准备模型 看起来很完美,我知道我还没有应用到最佳实践中。我现在只是想推出一个粗略的工作版本。谢谢你的帮助!
model = new ModelAndView("addEmployee", "employeeForm", employee);
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView postUser(@Valid @ModelAttribute("employeeForm" Employee employee, BindingResult result) {
ModelAndView model;
if (result.hasErrors()) {
model = new ModelAndView("addEmployee", result.getModel());
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String postUser(@Valid @ModelAttribute("employeeForm") Employee employee, BindingResult result, Model model) {
if (result.hasErrors()) {
return "addEmployee";
} else {
employeeService.addEmployee(employee);
model.addObject("employees", employeeService.getEmployees());
return "displayEmployee";
}
}