spring - Configuring a Conversion Service with a custom DateFormatter -


i'm trying add custom dateformatter spring/thymeleaf application, of following documentation : http://www.thymeleaf.org/doc/html/thymeleaf-spring3.html#conversions-utility-object

the problem don't use xml configuration beans definitions, webconfig.java class following implementation :

@configuration @enablewebmvc @componentscan(basepackages = {"com.myapp.web.controller","net.atos.wfs.wts.adminportal.web.domain"}) public class webconfig extends webmvcconfigureradapter {  private static final logger log = loggerfactory.getlogger(webconfig.class);   @override public void addresourcehandlers(resourcehandlerregistry registry) {     registry.addresourcehandler("/resources/**").addresourcelocations("/resources/"); }  @override public void addinterceptors(interceptorregistry registry) {     localechangeinterceptor localechangeinterceptor = new localechangeinterceptor();     localechangeinterceptor.setparamname("lang");     registry.addinterceptor(localechangeinterceptor); }  @bean public localeresolver localeresolver() {     cookielocaleresolver cookielocaleresolver = new cookielocaleresolver();     cookielocaleresolver.setdefaultlocale(stringutils.parselocalestring("en"));     return cookielocaleresolver; }  @bean public servletcontexttemplateresolver templateresolver() {     servletcontexttemplateresolver resolver = new servletcontexttemplateresolver();     resolver.setprefix("/web-inf/views/");     resolver.setsuffix(".html");     //nb, selecting html5 template mode.     resolver.settemplatemode("html5");     resolver.setcacheable(false);     return resolver;  }  public springtemplateengine templateengine() {     springtemplateengine engine = new springtemplateengine();     engine.settemplateresolver(templateresolver());     engine.setmessageresolver(messageresolver());     engine.adddialect(new layoutdialect());     return engine; }  @bean public viewresolver viewresolver() {      thymeleafviewresolver viewresolver = new thymeleafviewresolver();     viewresolver.settemplateengine(templateengine());     viewresolver.setorder(1);     viewresolver.setviewnames(new string[]{"*"});     viewresolver.setcache(false);     return viewresolver; }  @bean public imessageresolver messageresolver() {     springmessageresolver messageresolver = new springmessageresolver();     messageresolver.setmessagesource(messagesource());     return messageresolver; }  @override public void addformatters(formatterregistry registry) {     super.addformatters(registry);     registry.addformatter(new dateformatter()); }  @bean public messagesource messagesource() {      reloadableresourcebundlemessagesource messagesource = new reloadableresourcebundlemessagesource();     messagesource.setbasename("/web-inf/messages/messages");     // if true, key of message displayed if key not     // found, instead of throwing nosuchmessageexception     messagesource.setusecodeasdefaultmessage(true);     messagesource.setdefaultencoding("utf-8");     // # -1 : never reload, 0 reload     messagesource.setcacheseconds(0);     return messagesource; }   } 

and here code of custom dateformatter :

public class dateformatter implements formatter<date> {      @autowired     private messagesource messagesource;       public dateformatter() {         super();     }      public date parse(final string text, final locale locale) throws parseexception {         final simpledateformat dateformat = createdateformat(locale);         return dateformat.parse(text);     }      public string print(final date object, final locale locale) {         final simpledateformat dateformat = createdateformat(locale);         return dateformat.format(object);     }      private simpledateformat createdateformat(final locale locale) {          //the following line not working (nullpointerexception on messagesource)         //final string format = this.messagesource.getmessage("date.format", null, locale);         //the following line working :         final string format = "dd/mm/yyyy";         final simpledateformat dateformat = new simpledateformat(format);         dateformat.setlenient(false);         return dateformat;     }  } 

my question : how can add custom formatter able use @autowired elements ?

the xml configuration 1 :

<?xml version="1.0" encoding="utf-8"?> <beans ...>    ...       <mvc:annotation-driven conversion-service="conversionservice" />   ...    <!-- **************************************************************** -->   <!--  conversion service                                              -->   <!--  standard spring formatting-enabled implementation               -->   <!-- **************************************************************** -->   <bean id="conversionservice"         class="org.springframework.format.support.formattingconversionservicefactorybean">     <property name="formatters">       <set>         <bean class="thymeleafexamples.stsm.web.conversion.varietyformatter" />         <bean class="thymeleafexamples.stsm.web.conversion.dateformatter" />       </set>     </property>   </bean>    ...  </beans> 

i tried uset following configuration in webconfig class :

@bean     public formattingconversionservicefactorybean conversionservice() {         formattingconversionservicefactorybean conversionservice = new formattingconversionservicefactorybean();         set<formatter<?>> formatters = new treeset<formatter<?>>();         formatters.add(new dateformatter());         conversionservice.setformatters(formatters);         return conversionservice;     } 

but in case, formatter not taken account in application.

thanks in advance, antoine.

add webmvcconfigureradapter

@override public void addformatters(formatterregistry registry) {     registry.addformatter(dateformatter); }  @autowired private dateformatter dateformatter;  @bean public dateformatter dateformatter() {     return new dateformatter("dd/mm/yyyy"); } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -