在我的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这样的自定义方言。任何帮助都感激不尽。

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

最佳答案

我能找到的唯一方法是扩展SpringTemplateEngine(它被设计为可扩展的)。之后,用新的模板引擎替换default 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);
   }
}


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>

关于java - 如何向标准 thymeleaf 方言“th”添加自定义属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35597610/

10-12 02:04