简体   繁体   English

Spring MVC - 是否存在显示表单验证错误消息的替代方法(不使用@ModelAtrribute和@Valid)?

[英]Spring MVC -Is there alternative approach to display form validation error message (without using @ModelAtrribute along with @Valid)?

Trying to get Spring annotation based validation working in a simple webapp. 尝试在简单的Web应用程序中使用基于Spring注释的验证。 While using @ModelAttribute along with @Valid, I am getting validation error next to the field, however when I am adding object to model, not getting error messages. 在使用@ModelAttribute和@Valid时,我在字段旁边收到验证错误,但是当我向模型添加对象时,没有收到错误消息。

Suggest any alternative approach to display form validation error message(ie without using @ModelAtrribute along with @Valid) 建议显示表单验证错误消息的任何替代方法(即不使用@ModelAtrribute和@Valid)

Below is the code: 以下是代码:

EmployeeModel.java 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 + "]";
    }
}

Code snippet in Controller Controller中的代码片段

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

View: 视图:

<form:form action="save" method="post" modelAttribute="employee">
                <tr>
                    <td><form:label path="employee_id">Employee ID:</form:label></td>
                    <td><form:input path="employee_id" /></td>
                    <td><form:errors path="employee_id"/></td>
                </tr>
                <tr>
                    <td><form:label path="employee_name">Employee Name:</form:label></td>
                    <td><form:input path="employee_name" /></td>
                    <td><form:errors path="employee_name"/></td>
                </tr>
                <tr>
                    <td><form:label path="manager">Manager:</form:label></td>
                    <td><form:input path="manager" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit"
                        value="Save"></td>
                </tr>
            </form:form>

In case I use the commented line 如果我使用注释行

//model.addAttribute(employee)

I get exception stating: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute. 我得到异常声明:java.lang.IllegalStateException:BindingResult和bean名称'employee'的普通目标对象都不可用作请求属性。

Replacing the commented line with 用。替换注释行

model.addAttribute("employee",employee)

Page gets loaded with the previous value entered which means invalid object is not getting replaced, still error message is not displayed. 页面获取加载了先前输入的值,这意味着无效对象未被替换,仍然不显示错误消息。

The above can be achieved using @ModelAttribute along with @Valid, but is there any other alternative ie without using @ModelAtrribute? 以上可以使用@ModelAttribute和@Valid来实现,但还有其他选择,即不使用@ModelAtrribute吗?

What is the difference in working with @ModelAttribute and Model, as till now I considered both works in similar way? 使用@ModelAttribute和Model有什么区别,因为到目前为止我认为两者的工作方式类似?

@ModelAttribute populates the fields of a class, which then populates an attribute of the model to be passed back to the view. @ModelAttribute填充类的字段,然后填充要传递回视图的模型的属性。

Also you make following change on your saveEmployee method. 您还可以对saveEmployee方法进行以下更改。 You do not need to add attribute to model. 您不需要向模型添加属性。 @ModelAttribute automatically add for you. @ModelAttribute自动为您添加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM