@InitBinder
public void initBinder(WebDataBinder binder) {
// js html转义
binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
public void setAsText(String text) {
setValue(text == null ? null : StringEscapeUtils.escapeHtml3(text.trim()));
}
public String getAsText() {
Object value = getValue();
return value != null ? value.toString() : "";
}
});
// 时间处理
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false) {
private SimpleDateFormat dateFormat;
public void setAsText(String text) {
if (StringUtils.isBlank(StringUtils.trim(text))) {
setValue(null);
} else {
try {
if (text.length() == 10) {
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
} else {
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
setValue(this.dateFormat.parse(text));
} catch (ParseException ex) {
throw new IllegalArgumentException("时间转换异常", ex);
}
}
}
public String getAsText() {
Date value = (Date) getValue();
return value != null ? this.dateFormat.format(value) : "";
}
});
}