我有一张桌子employee
,它的属性是department
和。address
我想address
用 JSR 300 验证类的属性。但是我在验证地址和部门类的属性时遇到了问题。到目前为止,只Employee
验证了类的属性。我错过了什么吗?
public class Employee {
private Integer id;
@NotEmpty(message = "First Name must not be blank.")
@Size(min = 1, max = 50)
private String firstName;
@NotEmpty(message = "Last Name must not be blank.")
@Size(min = 1, max = 50)
private String lastName;
@Digits(message = "Salary field must not be blank.", integer=32767, fraction = 2)
@NumberFormat(style = Style.NUMBER)
@NotNull(message = "Salary field must not be blank.")
private Double salary;
@Digits(message = "Select department", integer=100, fraction = 0)
@NumberFormat(style = Style.NUMBER)
@NotNull(message = "Select department")
private Integer dept_id;
@NotEmpty(message = "Select gender")
private String gender;
private boolean employeeStatus;
private Address address;
private Department department;
}
public class Address {
private Integer id;
@NotEmpty(message = "Country must not be blank.")
@Size(min = 1, max = 50)
private String country;
@NotEmpty(message = "City must not be blank.")
@Size(min = 1, max = 50)
private String city;
@NotEmpty(message = "Street must not be blank.")
@Size(min = 1, max = 50)
private String street;
private Integer emp_id;
}
public class Department {
private String name;
private Integer id;
}
jsp
<form:form commandName="employee" action="addEmployee.htm"
name="employeeForm">
<form:errors path="*" cssClass="errorblock" element="div" />
<fieldset>
<form:label path="firstName">First Name</form:label>
<form:input path="firstName" />
<form:errors path="firstName" cssClass="error" />
<br>
<form:label path="lastName">Last Name</form:label>
<form:input path="lastName" />
<form:errors path="lastName" cssClass="error" />
<br>
<form:label path="address.country">Country</form:label>
<form:input path="address.country" />
<form:errors path="address.country" cssClass="error" />
<br>
<form:label path="address.city">City</form:label>
<form:input path="address.city" />
<form:errors path="address.city" cssClass="error" />
<br>
<form:label path="address.street">Street</form:label>
<form:input path="address.street" />
<form:errors path="address.street" cssClass="error" />
<br>
<form:label path="salary">Salary</form:label>
<form:input path="salary" />
<form:errors path="salary" cssClass="error" />
<br>
<form:label path="gender">Gender</form:label>
<form:radiobutton path="gender" value="Male" />
Male
<form:radiobutton path="gender" value="Female" />
Female
<form:errors path="gender" cssClass="error" />
<form:label path="dept_id">Department</form:label>
<form:select path="dept_id">
<form:option value="" label="Please Select"></form:option>
<form:options items="${departmentList}" itemValue="id"
itemLabel="name" />
</form:select>
<form:errors path="dept_id" cssClass="error" />
<label></label><input type="submit" value="Add Employee"><br>
</fieldset>
</form:form>
此外,当我在薪水字段中尝试随机字符串时,我得到了一个例外。
Failed to convert property value of type java.lang.String to required type java.lang.Double for property salary; nested exception is java.lang.NumberFormatException:
如何摆脱这个异常?