Tino M Thomas
Tino M Thomas

Reputation: 1746

How to add a custom attribute to standard thymeleaf dialect 'th'

In my spring MVC application I am trying to create a custom thymeleaf dialect to convert ASCII string to text. I am able to create the dialect with prefix other than 'th'. But if I try to use 'th' as prefix, then the server throwing the following runtime exception.

org.thymeleaf.exceptions.ConfigurationException: When using SpringTemplateEngine, at least one of the configured dialects must be or extend org.thymeleaf.spring4.dialect.SpringStandardDialect.
org.thymeleaf.spring4.SpringTemplateEngine.initializeSpecific(SpringTemplateEngine.java:147)
org.thymeleaf.TemplateEngine.initialize(TemplateEngine.java:831)
org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:203)
org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

Basically What I need to achieve here is to create a custom dialect like this th:asciitext. Any help will be much appreciated.

NB: If somebody needs to look into the code which I already tried, feel free to ask in the comments section.

Upvotes: 2

Views: 1937

Answers (2)

Aboodz
Aboodz

Reputation: 1584

The only way I could find is by extending SpringTemplateEngine (it is designed to be extended). After that, replace the defaultSpringTemplateEngine by the new template engine. Check out the code below:

MySpringTemplateEngine

public class MySpringTemplateEngine extends SpringTemplateEngine {

   private Set<IProcessor> additionalProcessors;

   public Set<IProcessor> getAdditionalProcessors() {
      return additionalProcessors;
   }

   public void setAdditionalProcessors(Set<IProcessor> additionalProcessors) {
      this.additionalProcessors = additionalProcessors;
   }

   @Override
   public void afterPropertiesSet() throws Exception {
      super.afterPropertiesSet();
      Map<String, IDialect> dialectsByPrefix = this.getDialectsByPrefix();
      StandardDialect springDialect = (StandardDialect) dialectsByPrefix.get("th");
      springDialect.setAdditionalProcessors(additionalProcessors);
   }
}

application-context.xml

  <bean id="templateEngine" class="path.to.extended.engine.MySpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <property name="additionalProcessors">
      <set>
          <bean class="path.to.your.processor.AttrProcessor1" />
          <bean class="path.to.your.processor.AttrProcessor2" />
      </set>
    </property>
  </bean>

Upvotes: 2

Prashant
Prashant

Reputation: 5383

Please refer to this URL: http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

I think you have a similar scenario as Scenario 1.

Upvotes: 1

Related Questions