addEmployee.jsp
<form:form id="bookForm" modelAttribute="employee" method="post" action="${baseURL}employee/saveemployee" class="form-horizontal" enctype="multipart/form-data">
<div class="form-group">
<label for="publishedOn" class="col-xs-4 control-label">Birth Date
</label>
<div class="col-xs-8">
<form:input path="personalInformation.dateOfBirth" placeholder="yyyy-MM-dd" class="datepicker form-control" />
<form:errors path="personalInformation.dateOfBirth"></form:errors>
</div>
</div>
<div class="form-group">
<label for="dateOfJoining" class="col-xs-4 control-label">Date Of Joining</label>
<div class="col-xs-8">
<form:input path="dateOfJoining" placeholder="dateOfJoining" class="form-control" />
<form:errors path="dateOfJoining"></form:errors>
</div>
</div>
</form:form>
2)EmployeeAddController.java
@RequestMapping(value="/saveemployee",method = RequestMethod.POST)
public String saveEmployee(@ModelAttribute("employee")Employee employee,BindingResult result,Errors e,ModelMap map){
employeeValidatorobj.validate(employee,e);
if(e.hasErrors())
{
map.addAttribute("employee",employee);
return "addEmployee";
}
else{
System.out.println("inside save");
System.out.println(employee.getPersonalInformation().getFirstName());
map.addAttribute("employee",new Employee());
iEmployeeService.saveEmployee(employee);
map.addAttribute("message", "EmployeeAdded sucessfully");
return "employeeList";
}
}
这是控制器,我正在检查表单是否有错误。
3)emolyeeValidator.java
public class EmployeeValidator implements Validator {
private Pattern pattern;
private Matcher matcher;
private static final String DATE_PATTERN="((?:19|20)\\d\\d)/(0?[1-9]|1[012])/([12][0-9]|3[01]|0?[1-9])";
@Override
public boolean supports(Class<?> arg0) {
return Employee.class.equals(arg0);
}
@Override
public void validate(Object obj, Errors errors) {
// Employee employee=new Employee();
Employee employee = (Employee) obj;
String date=employee.getDateOfJoining().toString();
if (!date.matches(DATE_PATTERN)) {
pattern=pattern.compile(DATE_PATTERN);
matcher=pattern.matcher(date);
if(matcher.matches()){
errors.rejectValue("dateOfJoining", "dateOfJoining.incorrect","enter correct dateOfJoining");
}
}
这是employeeValidator类,用于日期的客户验证,但是当我尝试保存记录时,它会给出空指针exception.i还映射了bean中的employeevalidator。每当我尝试使用.equals()字符串方法进行验证时,它会给出jsp页面字符串上的异常无法转换为java.util.Date.please检查代码并建议我如何在employeeValidator中检查日期格式的条件(YYYY-MM-DD)