Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 SpringMVC-是否有其他方法显示表单验证错误消息(不使用@modelatribute和@Valid)?_Java_Spring_Jsp_Spring Mvc_Spring Form - Fatal编程技术网

Java SpringMVC-是否有其他方法显示表单验证错误消息(不使用@modelatribute和@Valid)?

Java SpringMVC-是否有其他方法显示表单验证错误消息(不使用@modelatribute和@Valid)?,java,spring,jsp,spring-mvc,spring-form,Java,Spring,Jsp,Spring Mvc,Spring Form,试图在一个简单的webapp中实现基于Spring注释的验证。当使用@modeldattribute和@Valid时,我会在字段旁边得到验证错误,但是当我向模型添加对象时,不会得到错误消息 建议显示表单验证错误消息的任何替代方法(即不使用@modelatribute和@Valid) 代码如下: EmployeeModel.java @Entity @Table(name = "Employee") public class EmployeeModel implements Serializabl

试图在一个简单的webapp中实现基于Spring注释的验证。当使用@modeldattribute和@Valid时,我会在字段旁边得到验证错误,但是当我向模型添加对象时,不会得到错误消息

建议显示表单验证错误消息的任何替代方法(即不使用@modelatribute和@Valid)

代码如下:

EmployeeModel.java

@Entity
@Table(name = "Employee")
public class EmployeeModel implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    @NotNull(message = "ID is required")
    private Integer employee_id;

    @Column(name = "Name")
    @NotEmpty(message = "Name is required")
    private String employee_name;

    @Column(name = "Manager")
    private String manager;

    public Integer getEmployee_id() {
        return employee_id;
    }

    public void setEmployee_id(Integer employee_id) {
        this.employee_id = employee_id;
    }

    public String getEmployee_name() {
        return employee_name;
    }

    public void setEmployee_name(String employee_name) {
        this.employee_name = employee_name;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }

    @Override
    public String toString() {
        return "Employee [id=" + employee_id + ", Name=" + employee_name
                + ", Manager=" + manager + "]";
    }
}
控制器中的代码段

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveEmployee(Model model, @Valid EmployeeModel employee, BindingResult result) {
    if (result.hasErrors()) {
        model.addAttribute("employee", employee);
        //model.addAttribute(employee);
        return "EmployeeForm";
    }
    employeeService.addEmployee(employee);
    return "forward:/";
}
视图:

我得到了异常声明:java.lang.IllegalStateException:bean名称“employee”的BindingResult和普通目标对象都不能作为请求属性使用

将注释行替换为

model.addAttribute("employee",employee)
页面加载时输入了上一个值,这意味着不会替换无效对象,但仍不会显示错误消息

可以使用@modeldattribute和@Valid实现上述功能,但是否有其他替代方法,即不使用@modeldattribute

使用@modeldattribute和Model有什么区别?到目前为止,我认为这两种方法的工作原理是相似的

@ModelAttribute填充类的字段,然后填充要传递回视图的模型属性

您还可以对saveEmployee方法进行以下更改。您不需要向模型添加属性
@modeldattribute
自动为您添加

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveEmployee(@Valid @ModelAttribute(name = "employee") EmployeeModel employee, BindingResult result) {
    if (result.hasErrors()) {
        return "EmployeeForm";
    }
    employeeService.addEmployee(employee);
    return "forward:/";
}
@ModelAttribute填充类的字段,然后填充要传递回视图的模型属性

您还可以对saveEmployee方法进行以下更改。您不需要向模型添加属性
@modeldattribute
自动为您添加

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveEmployee(@Valid @ModelAttribute(name = "employee") EmployeeModel employee, BindingResult result) {
    if (result.hasErrors()) {
        return "EmployeeForm";
    }
    employeeService.addEmployee(employee);
    return "forward:/";
}

模型
绑定结果
都是模型。但是,当使用
模型时
会丢失
BindingResult
中提供的所有附加信息。简单地说,不要两者兼用,你所做的是你不应该做的事情。您实际使用的不是模型,而是刚构造的对象。在
@Valid
旁边,将
@modeldattribute(“employee”)
添加到方法中,并删除
Model
参数。重新启动…是否有其他方法显示验证错误消息(不使用@modeldattribute)不要使用
Model
并使用
result.getModel()。添加(“employee”,employee)
可能有效。但为什么要让它变得更复杂,让Spring来为您处理。我以前也遇到过同样的问题。假设我不想同时使用Valid和ModelAttribute(“employee”)注释,是否可以只使用@Valid,并以编程方式将模型传递到modelmap中,以便表单能够显示错误消息。如果有人问我,我为什么要这样做,我的回答是,如果可能的话,只是想知道,为了学习的目的。如果你因为某种原因不能使用@modeldattribute(“employee”),你可以试试这个。它只适合我。。。在参数中,包括ModelMap mm。然后执行此操作,mm.put(BindingResult.class.getName()+“.employee”,result);
模型
绑定结果
都是模型。但是,当使用
模型时
会丢失
BindingResult
中提供的所有附加信息。简单地说,不要两者兼用,你所做的是你不应该做的事情。您实际使用的不是模型,而是刚构造的对象。在
@Valid
旁边,将
@modeldattribute(“employee”)
添加到方法中,并删除
Model
参数。重新启动…是否有其他方法显示验证错误消息(不使用@modeldattribute)不要使用
Model
并使用
result.getModel()。添加(“employee”,employee)
可能有效。但为什么要让它变得更复杂,让Spring来为您处理。我以前也遇到过同样的问题。假设我不想同时使用Valid和ModelAttribute(“employee”)注释,是否可以只使用@Valid,并以编程方式将模型传递到modelmap中,以便表单能够显示错误消息。如果有人问我,我为什么要这样做,我的回答是,如果可能的话,只是想知道,为了学习的目的。如果你因为某种原因不能使用@modeldattribute(“employee”),你可以试试这个。它只适合我。。。在参数中,包括ModelMap mm。然后执行此操作,mm.put(BindingResult.class.getName()+“.employee”,result);是否有其他方法显示验证错误消息(不使用@modeldattribute)?我也对spring感到不满。如果有任何其他替代方法,我不知道我的回答是否有助于您看到以下内容:是否有任何替代方法显示验证错误消息(不使用@ModelAttribute)?我也对spring感到不满。如果有其他备选方案,我不知道我的回答是否有助于您了解以下内容:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveEmployee(@Valid @ModelAttribute(name = "employee") EmployeeModel employee, BindingResult result) {
    if (result.hasErrors()) {
        return "EmployeeForm";
    }
    employeeService.addEmployee(employee);
    return "forward:/";
}