gpt4 book ai didi

java - 如何使用 Spring MVC JSR-303 validator 将不同的模型类验证为一种形式

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:10:30 25 4
gpt4 key购买 nike

我在 jsp 页面中有如下表单:

<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>

在此 jsp 中,名字、姓氏和电子邮件属于模型类,即 Employee。最后,街道、城市等所有属性都属于 EmployeeDetails 模型类。

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
}

员工详情:

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
}

我在单个表单中对两个模型执行验证。我的 Cotroller 映射是:

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

但是收到 Bad Request 是行不通的。 enter image description here

最佳答案

要完成这项工作,您应该在 Employee.class 中使用 @Valid 注释 employeeDetails,如下所示:

@Valid
private EmployeeDetail employeeDetail;

现在在 Controller 中你应该像这样使用 Employee 对象:

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

关于java - 如何使用 Spring MVC JSR-303 validator 将不同的模型类验证为一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com