Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带ModelAndView的Spring MVC验证_Java_Spring_Validation_Spring Mvc - Fatal编程技术网

Java 带ModelAndView的Spring MVC验证

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

我正在尝试将验证添加到我的SpringMVC应用程序中。在尝试设置验证之前,我一直在使用ModelAndView来提供jsp页面,但没有显示错误消息

模型

控制器

@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";
    }
}