技术:spring-5.0.0.RELEASE
运行环境:IDEA 15.2 + jdk8 + windows10 + spring-5.0.0.RELEASE
demo功能:提供一个spring mvc 绑定 date 类型数据的demo
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by francis on 2018/1/4.
*/
public class DateEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(text);
} catch (ParseException e) {
format = new SimpleDateFormat("yyyy-MM-dd");
try {
date = format.parse(text);
} catch (ParseException e1) {
e1.printStackTrace();
}
}
setValue(date);
}
}
import com.ctrip.datebinder.DateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController;
import java.lang.annotation.Annotation;
import java.util.Date;
/**
* Created by francis on 2018/1/4.
*/
@RestController
public class BaseController implements Controller {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DateEditor());
}
@Override
public String value() {
return null;
}
@Override
public Class<? extends Annotation> annotationType() {
return null;
}
}
@RestController
@RequestMapping("/")
public class IndexController extends BaseController {
@RequestMapping("1")
public String index(Date date) {
System.out.println(date);
return "hi";
}
}
欢迎添加微信,互相学习↑↑↑ -_-
白老虎
programming is not only to solve problems, ways to think
grafana 级连 菜单 templating (variables) 配置
rocketmq 集群搭建 (2master + 2slave + 2namesrv)
AI 机器人 抓取 微信 聊天中的 百度网盘 分享地址和密码