因为springmvc默认是date参数不能为空,传参时如果想为空就可以在Controller【Action】处理页面写上以下代码就好
import org.springframework.web.bind.WebDataBinder;
@org.springframework.web.bind.annotation.InitBinder
public void InitBinder(WebDataBinder dataBinder)
{
dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
public void setAsText(String value) {
try {
setValue(new SimpleDateFormat("yyyy/MM/dd").parse(value));
} catch(Exception e) {
setValue(null);
}
}
public String getAsText() {
return new SimpleDateFormat("yyyy/MM/dd").format((Date) getValue());
}
});
}
本文介绍如何在SpringMVC中通过自定义日期编辑器实现日期参数允许为空的功能。通过注册一个针对Date类型的定制编辑器,可以使得传入的日期字符串在解析失败时被设为null。
732

被折叠的 条评论
为什么被折叠?



