Guest User

Untitled

a guest
Oct 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. // init the template engine
  2. templateEngine = new SpringTemplateEngine();
  3. templateResolver = new ClassLoaderTemplateResolver();
  4. templateEngine.addTemplateResolver(templateResolver);
  5.  
  6. // generate the template
  7. Context ctx = new Context(locale);
  8. // i would like, for example, to format dates
  9. ctx.setVariable("date", new Date());
  10. String text = this.templateEngine.process(templateName, ctx);
  11.  
  12. Set<IDialect> dialects = this.templateEngine.getDialects();
  13. StandardDialect standardDialect = (StandardDialect) dialects.iterator().next();
  14. IStandardConversionService conversionService = new MyConversionService();
  15. standardDialect.setConversionService(conversionService);
  16.  
  17. public MyConversionService implements IStandardConversionService {
  18. GenericConversionService myConverter = new MyConverter();
  19. StandardConversionService standardConversionService = new StandardConversionService();
  20.  
  21. @Override
  22. public <T> T convert(Configuration configuration, IProcessingContext processingContext, Object object, Class<T> targetClass) {
  23.  
  24. if (myConverter.canConvert(object.getClass(), targetClass)) {
  25. return myConverter.convert(object, targetClass);
  26. }
  27.  
  28. return standardConversionService.convert(configuration, processingContext, object, targetClass);
  29. }
  30. }
  31.  
  32. ${{variable}}
  33.  
  34. <dependency>
  35. <groupId>org.thymeleaf</groupId>
  36. <artifactId>thymeleaf-spring4</artifactId>
  37. <version>2.1.4.RELEASE</version>
  38. </dependency>
  39.  
  40. public class DateFormatter implements Formatter<Date> {
  41.  
  42. public DateFormatter() {
  43. super();
  44. }
  45.  
  46. @Override
  47. public Date parse(final String text, final Locale locale) throws ParseException {
  48. final SimpleDateFormat dateFormat = createDateFormat(locale);
  49. return dateFormat.parse(text);
  50. }
  51.  
  52. @Override
  53. public String print(final Date object, final Locale locale) {
  54. final SimpleDateFormat dateFormat = createDateFormat(locale);
  55. return dateFormat.format(object);
  56. }
  57.  
  58. private SimpleDateFormat createDateFormat(final Locale locale) {
  59. final String format = "dd/MM/yyyy";
  60. final SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
  61. dateFormat.setLenient(false);
  62. return dateFormat;
  63. }
Add Comment
Please, Sign In to add comment