How to Validate different model class into one form using Spring MVC JSR-303 Validator

筅森魡賤 提交于 2019-12-06 09:43:32

问题


I have form inside jsp page as below:

<springForm:form action="${addAction}" name="frm" method="post" commandName="employee">
    <table>
        <tr>
            <td><label>First Name</label>   </td>
            <td><springForm:input path="firstName" /></td>
            <td><springForm:errors path="firstName" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Last Name</label></td>
            <td><springForm:input path="lastName" /></td>
            <td><springForm:errors path="lastName" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Email</label>    </td>
            <td><springForm:input path="email" /></td>
            <td><springForm:errors path="email" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Street</label>   </td>
            <td><springForm:input path="employeeDetail.street" /></td>
            <td><springForm:errors path="employeeDetail.street" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>City</label> </td>
            <td><springForm:input path="employeeDetail.city" /></td>
            <td><springForm:errors path="employeeDetail.city" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>State</label>    </td>
            <td><springForm:input path="employeeDetail.state" /></td>
            <td><springForm:errors path="employeeDetail.state" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Country</label>  </td>
            <td><springForm:input path="employeeDetail.country" /></td>
            <td><springForm:errors path="employeeDetail.country" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="submit" value="Add Info" />
            </td>
        </tr>
    </table>
</springForm:form>

In this jsp First Name, Last Name and email belong to a model class i.e Employee. Last all attributes like Street, City etc belong to EmployeeDetails model class.

public class Employee {

private Long empId;

@Size(min=2, max=30)
private String firstName;

@Size(min=2, max=30)
private String lastName;

@NotEmpty @Email
private String email;

@DateTimeFormat(pattern="MM/dd/yyyy")
@NotNull @Past
private Date doj;

@Phone
private String phone;

private EmployeeDetail employeeDetail;

Getter/Setter
}

EmployeeDetail:

public class EmployeeDetail {

@Size(min=2, max=30)
private String street;

@Size(min=2, max=30)
private String city;

@Size(min=2, max=30)
private String state;

@Size(min=2, max=30)
private String country;
//Getter/Setter
}

I am performing validation on both model inside a single form. My Cotroller mapping is:

@RequestMapping(value = "/saveEmpInfo", method = RequestMethod.POST)
public String saveEmployee(@Valid Employee employee,
        @Valid EmployeeDetail employeeDetail, BindingResult bindingResult) {
    employee.setEmployeeDetail(employeeDetail);
    if (bindingResult.hasErrors()) {
        System.out.println("Returning addInfo.jsp page");
        return "addInfo";
    }
    //future logic
}

But It's not working getting Bad Request.


回答1:


To make this work you should annotate employeeDetails with @Valid in your Employee.class like this:

@Valid
private EmployeeDetail employeeDetail;

And now in controller you should use just Employee object like this:

public String saveEmployee(@Valid Employee employee, BindingResult bindingResult) {



回答2:


You should change the names of the attributes belong to EmployeeDetail from e.g. name="street" to name="employeeDetail.street"

Also, you should add a BindingResult object right behind the Employee argument as well, so that the framework can store the binding error related to Employee, the current BindingResult will apply only to EmployeeDetail instance, so

saveEmployee(@Valid Employee employee, BindingResult bindingResultEmployee,
        @Valid EmployeeDetail employeeDetail, BindingResult bindingResult) 

without it you're getting a bad request, with it, the request will actually reach the method, and the logs will show the exact binding error if set to debug level



来源:https://stackoverflow.com/questions/31561477/how-to-validate-different-model-class-into-one-form-using-spring-mvc-jsr-303-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!