@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中给出今天的日期?它只需要恒定。
最佳答案
@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
关于spring - Spring 如何在requestparam中提供默认日期值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17693435/