Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // init the template engine
- templateEngine = new SpringTemplateEngine();
- templateResolver = new ClassLoaderTemplateResolver();
- templateEngine.addTemplateResolver(templateResolver);
- // generate the template
- Context ctx = new Context(locale);
- // i would like, for example, to format dates
- ctx.setVariable("date", new Date());
- String text = this.templateEngine.process(templateName, ctx);
- Set<IDialect> dialects = this.templateEngine.getDialects();
- StandardDialect standardDialect = (StandardDialect) dialects.iterator().next();
- IStandardConversionService conversionService = new MyConversionService();
- standardDialect.setConversionService(conversionService);
- public MyConversionService implements IStandardConversionService {
- GenericConversionService myConverter = new MyConverter();
- StandardConversionService standardConversionService = new StandardConversionService();
- @Override
- public <T> T convert(Configuration configuration, IProcessingContext processingContext, Object object, Class<T> targetClass) {
- if (myConverter.canConvert(object.getClass(), targetClass)) {
- return myConverter.convert(object, targetClass);
- }
- return standardConversionService.convert(configuration, processingContext, object, targetClass);
- }
- }
- ${{variable}}
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf-spring4</artifactId>
- <version>2.1.4.RELEASE</version>
- </dependency>
- public class DateFormatter implements Formatter<Date> {
- public DateFormatter() {
- super();
- }
- @Override
- public Date parse(final String text, final Locale locale) throws ParseException {
- final SimpleDateFormat dateFormat = createDateFormat(locale);
- return dateFormat.parse(text);
- }
- @Override
- public String print(final Date object, final Locale locale) {
- final SimpleDateFormat dateFormat = createDateFormat(locale);
- return dateFormat.format(object);
- }
- private SimpleDateFormat createDateFormat(final Locale locale) {
- final String format = "dd/MM/yyyy";
- final SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
- dateFormat.setLenient(false);
- return dateFormat;
- }
Add Comment
Please, Sign In to add comment