2017-05-24 1 views
0

1) à la page jsp:Date d'envoi de datepicker bootstrap au contrôleur Spring avec TimeZone

<input class="datepicker"/>  -- 10.02.2017 

jQuery:

$('.datepicker').datepicker({ 
     'format': 'dd.mm.yyyy', 
    }); 

2) Contrôleur - Test.java

@RequestMapping("test") 
public ModelAndView test(@PathVariable Date testDate){ 
     // testDate = 09.02.2017 23:00:00 
} 

Exemple : Je choisis dans datapicker (bootstrap) 10.02.2017 (№1) et obtenir testDate dans le contrôleur (№2) 09.02.2017 23:00:00.

Je pense que Spring ou java.util.Date est converti testDate en serveur TimeZone. Mais je peux me tromper

Comment envoyer la valeur Date à Spring Controller à partir de bootstrap datepicker sur jsp avec le client TimeZone? Je devrais obtenir 10.02.2017

Répondre

0

J'ai trouvé la solution pour moi. Valeur du type de date convertie Spring. J'ai écrit:

public class DateConverter implements Converter<String, Date> { 

@Override 
public Date convert(String source) { 
    SimpleDateFormat sf = new SimpleDateFormat("dd.MM.yyyy"); 
    if (!source.isEmpty()){ 
     try { 
     return sf.parse(source); 
     } catch (ParseException e) { 
     LOGGER.error(e.getLocalizedMessage()); 
     } 
    } 
    return null; 
} 

et ajouter à la classe @Configuration

@Override 
public void addFormatters(FormatterRegistry registry) { 
    registry.addConverter(new DateConverter()); 
} 

Bien sûr peut remplacer metod convertir différemment