【问题标题】:How to add a custom attribute to standard thymeleaf dialect 'th'如何向标准百里香方言“th”添加自定义属性
【发布时间】:2016-02-24 09:04:38
【问题描述】:

在我的 spring MVC 应用程序中,我正在尝试创建一个自定义 thymeleaf 方言来将 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 这样的自定义方言。任何帮助将不胜感激。

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

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

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

    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>
    

    【讨论】:

      【解决方案2】:

      请参考这个网址:http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

      我认为您的情况与场景 1 类似。

      【讨论】:

      • 我已经提到了这个链接。没有给出任何例子可以参考
      • 这是他们定制的 extrathyme 应用程序(他们使用的是 spring mvc):github.com/thymeleaf/thymeleafexamples-extrathyme.git 我同意这可能需要一些时间来解决我们的问题,但这是我开始的地方。
      • 您在答案中提到的链接中已经提供了此示例。但这都是关于前缀不是“th”的方言
      • th 是 thymeleaf 标准方言使用的标准前缀。我不确定您是否有很多自定义选项。
      • 根据上述链接的场景1,这是可能的,但没有给出任何示例。
      猜你喜欢
      • 2015-03-15
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2015-01-27
      • 2018-02-21
      • 1970-01-01
      相关资源
      最近更新 更多