java 带有日期的 Spring MVC 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16846763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC controller with date
提问by user812612
I've been trying to use:
我一直在尝试使用:
@RequestMapping(value="/consultaporusuarioperiodo/{idusuario}/{datainicio}/{datafim}", method = RequestMethod.GET)
public String consultaPorPeriodoUsuario(
@PathVariable("idusuario") Long idUsuario,
@PathVariable("datainicio") Date dataInicio,
@PathVariable("datafim") Date dataFim
,Model model) {
Usuario usuario = usuarioService.buscaPorId(idUsuario);
model.addAttribute("timesheet",timeSheetService.buscaPorPeriodoPorUsuario(dataInicio, dataFim,usuario));
return "timesheetcrud/consultatimesheet";
}
with this link:
用这个链接:
http://localhost:8080/timesheet/consultaporusuarioperiodo/1/21012000/22012000
but I get this error:
但我收到此错误:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect ().
Apache Tomcat/7.0.27
when I change to:
当我改为:
@PathVariable("datainicio") String dataInicio,
@PathVariable("datafim") String dataFim
It's work. What can I do to work with Date ?
这是工作。我可以做什么来使用 Date ?
thanks
谢谢
回答by dimitrisli
Try:
尝试:
@PathVariable("datainicio") @DateTimeFormat(iso=ISO.DATE) String dataInicio,
@PathVariable("datafim") @DateTimeFormat(iso=ISO.DATE) String dataFim
where ISO.DATE
is of yyyy-mm-dd
pattern.
哪里ISO.DATE
是yyyy-mm-dd
模式。
回答by Noah Hall-Potvin
I had to do something very similar. This is what I did:
我不得不做一些非常相似的事情。这就是我所做的:
@PathVariable("datainicio") @DateTimeFormat(pattern = "ddMMyyyy") Date dataInicio
Hope this helps!
希望这可以帮助!
回答by Paul T
In Spring's spirit, the best choice should be a convention to impose a unified way of handling Date parameters. And luckily it gives you exactly this little cookie - drop it in application.properties and ... Bob's your uncle:
在 Spring 的精神中,最好的选择应该是一个约定,以统一的方式处理 Date 参数。幸运的是,它给了你这个小饼干——把它放在 application.properties 中,然后......鲍勃是你的叔叔:
spring.mvc.date-format=ddMMyyyy
spring.mvc.date-format=ddMMyyyy