通过InitBinder注解,做到全局的格式化转换
2015-12-09 10:21
405 查看
首先自定义一个格式转换类(我们以Date格式为例)DateFormatEditor继承自PropertiesEditor:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.PropertiesEditor;
public class DateFormatEditor extends PropertiesEditor {
// 日期是否允许为空
private boolean isAllowEmpty;
// 日期格式
private String pattern;
public DateFormatEditor() {
super();
this.isAllowEmpty = true;
this.pattern = "yyyy-MM-dd";
}
public DateFormatEditor(String pattern, boolean isAllowEmpty) {
super();
this.isAllowEmpty = isAllowEmpty;
this.pattern = pattern;
}
public boolean isAllowEmpty() {
return isAllowEmpty;
}
public void setAllowEmpty(boolean isAllowEmpty) {
this.isAllowEmpty = isAllowEmpty;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
if (text != null && !text.equals("")) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
setValue(format.parse(text));
} else {
if (isAllowEmpty) {
setValue(null);
}
}
} catch (ParseException e) {
System.out.println("格式转换失败!");
setValue(null);
}
}
}定义完了还是不能用,这里需要我们注册:
由于需要全局的格式化转换所以我们定义一个BaseController类:
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import spring.util.DateFormatEditor;
@Controller
public class BaseController {
@Autowired
protected HttpServletRequest request;
@Autowired
protected HttpServletResponse response;
@Autowired
protected HttpSession session;
@Autowired
protected ServletContext application;
@InitBinder
protected void initDate(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
}
}这样就把所有Date类型的参数全部拦截下来进行判断和格式化。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.PropertiesEditor;
public class DateFormatEditor extends PropertiesEditor {
// 日期是否允许为空
private boolean isAllowEmpty;
// 日期格式
private String pattern;
public DateFormatEditor() {
super();
this.isAllowEmpty = true;
this.pattern = "yyyy-MM-dd";
}
public DateFormatEditor(String pattern, boolean isAllowEmpty) {
super();
this.isAllowEmpty = isAllowEmpty;
this.pattern = pattern;
}
public boolean isAllowEmpty() {
return isAllowEmpty;
}
public void setAllowEmpty(boolean isAllowEmpty) {
this.isAllowEmpty = isAllowEmpty;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
if (text != null && !text.equals("")) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
setValue(format.parse(text));
} else {
if (isAllowEmpty) {
setValue(null);
}
}
} catch (ParseException e) {
System.out.println("格式转换失败!");
setValue(null);
}
}
}定义完了还是不能用,这里需要我们注册:
由于需要全局的格式化转换所以我们定义一个BaseController类:
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import spring.util.DateFormatEditor;
@Controller
public class BaseController {
@Autowired
protected HttpServletRequest request;
@Autowired
protected HttpServletResponse response;
@Autowired
protected HttpSession session;
@Autowired
protected ServletContext application;
@InitBinder
protected void initDate(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
}
}这样就把所有Date类型的参数全部拦截下来进行判断和格式化。
相关文章推荐
- My97DatePicker相关笔记(未完待续)
- 【转稿】:loser的共同特征
- html5学习:轮播图
- 夺命雷公狗---javascript NO:29 Object类
- Android递归方式删除某文件夹下的所有文件
- 04 实现图像与键盘的交互操作-DirectX游戏开发
- 蜂鸣器驱动电路 自主开发设计
- SQLserver中的xp_cmdshell
- PHPStorm 10 激活
- 夺命雷公狗---javascript NO:28 原型链
- [转载]内核初始化优化宏(__init, __devinit)
- Http1.1特定的状态码
- 完美实现bootstrap分页查询
- Android调用系统安装/卸载apk过程
- Android 编译系统 (二)
- TSQL方法获取服务器磁盘空间信息
- 《C语言综合研究第1章宣讲会研究报告_20130609_v1.0》
- 项目管理实践教程一、工欲善其事,必先利其器【Basic Tools】
- 带有×的EditText
- 将MySQL查询结果按值排序的简要教程