Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC controller with date

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 ?

thanks

like image 213
user812612 Avatar asked May 30 '13 22:05

user812612


2 Answers

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.

like image 189
dimitrisli Avatar answered Oct 23 '22 13:10

dimitrisli


I had to do something very similar. This is what I did:

@PathVariable("datainicio") @DateTimeFormat(pattern = "ddMMyyyy") Date dataInicio

Hope this helps!

like image 22
Noah Hall-Potvin Avatar answered Oct 23 '22 13:10

Noah Hall-Potvin