, spring, , , - jQuery DatePicker InitBinder.
JS :
<form:input cssClass="datepicker" path="someProperty" readonly="true" />
JS:
$(document).ready(function() {
$('.datepicker').datepicker();
});
:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
Mismatch , , . , jQuery DatePicker, ( , MM/dd/yyyy). , , Spring CustomEditor String . , BindingResults ( ). , , Mismatch.
: , ...
Bean -, ( View to Controller). , .
public class SomeBean {
private Date someDate;
public Date getSomeDate() { return someDate; }
public void setSomeDate(Date date) { somedate = date; }
}
. Model
@SessionAttribute.
@Controller
@RequestMapping("/somePath")
@SessionAttributes({"someFormBean"})
public class SomeController {
@RequestMapping()
public String defaultView(@ModelAttribute("someFormBean") SomeBean someFormBean, Model uiModel) {
return "someDefaultView";
}
@RequestMapping(method = RequestMethod.POST
public String submit(
@ModelAttribute("someFormBean") SomeBean someFormBean,
BindingResult bindingResults,
Model uiModel) {
if(bindingResults.hasErrors()) {
return "errorView";
} else {
return "successView";
}
}
@ModelAttribute("someFormBean")
public SomeBean createSomeBean() {
return new SomeBean();
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
}
( "someDefaultView" , JSP , Spring JSTL)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<script type="text/javascript" src="/resources/jquery/1.7.1/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="resources/jquery.ui/1.8.13/jquery.ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.datepicker').datepicker();
};
</script>
</head>
<body>
<form:form modelAttribute="someFormBean">
<form:input cssClass="datepicker" path="someDate" readonly="true" /><br />
<form:errors path="a"/><br /><br />
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
, Google'ing Spring Init Binders, cusomizing binding errors (typeMismatch) JSR 303 , . , , , , , , . , , , 20 , . , .