如何为标准的百里香方言添加自定义属性' th'

时间:2016-02-24 09:04:38

标签: java spring-mvc thymeleaf

在我的spring MVC应用程序中,我正在尝试创建一个自定义百万美元方言来将ASCII字符串转换为文本。我能够使用'th'以外的前缀创建方言。但是,如果我尝试使用'th'作为前缀,那么服务器将抛出以下运行时异常。

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)

基本上我需要在这里创建一个像th:asciitext这样的自定义方言。任何帮助将不胜感激。

注意:如果有人需要查看我已尝试的代码,请随时在评论部分询问。

2 个答案:

答案 0 :(得分:2)

我能找到的唯一方法是扩展SpringTemplateEngine(它旨在扩展)。之后,用新模板引擎替换默认的SpringTemplateEngine。看看下面的代码:

<强> 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);
   }
}

<强>应用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>

答案 1 :(得分:1)

请参阅以下网址:http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

我认为你有与场景1类似的场景。