本文介绍了春天如何在requestparam中提供默认日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@RequestMapping(value = "/getSettlements", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
Collection<Settlement> getSettlements
(@RequestParam(value = "startDate") String startDate,
@RequestParam(value = "endDate") String endDate,
@RequestParam(value = "merchantIds", defaultValue = "null") String merchantIds)
如何在defaultValue中给出今天的日期?只需常数.
How to give today's date in defaultValue ? It only takes constant.
推荐答案
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
final CustomDateEditor dateEditor = new CustomDateEditor(df, true) {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if ("today".equals(text)) {
setValue(new Date());
} else {
super.setAsText(text);
}
}
};
binder.registerCustomEditor(Date.class, dateEditor);
}
@RequestParam(required = false, defaultValue = "today") Date startDate
这篇关于春天如何在requestparam中提供默认日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!