spring之Thymeleaf 3 和 Tiles2 集成

oomusou 阅读:80 2025-06-02 22:19:02 评论:0

Thymeleaf 3 是否以某种方式支持 Tiles 2?有一个我用于 Thumeleaf 2.x.x 的包 thymeleaf-extras-tiles2-spring4但正如我现在看到的那样,由于 org.thymeleaf.dialect.AbstractDialect 中的更改,它不兼容类

Caused by: java.lang.NoSuchMethodError: org.thymeleaf.dialect.AbstractDialect: method <init>()V not found 
[INFO]  at org.thymeleaf.extras.tiles2.dialect.TilesDialect.<init>(TilesDialect.java:46) 

我是否需要等待此集成的更新才能开始使用 T3?

有什么方法可以在 Thymeleaf3 中模拟 Tiles

我只将我的 Tiles 用于这样的事情:

  <definition name="portal/**" template="layouts/portal"> 
    <put-attribute name="_head" value="/portal/{1} :: _head"/> 
    <put-attribute name="content" value="/portal/{1} :: content"/> 
  </definition> 

请您参考如下方法:

为了解决这个问题,我为 SpringTemplateEngine 创建了一个代理,并建议使用 TemplateEngine.process() 方法。

工作原理:

基于模板路径的代码映射布局实例:

portal/moje_konto/moje_dane 

映射到布局

 LAYOUTS_PATH/portal 

此外,它传递包含实际模板路径的变量 VIEW

内部布局可用于包含特定片段。 非常简单的门户布局可能如下所示:

<!DOCTYPE html SYSTEM "about:legacy-compat"> 
<html lang="pl" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.springframework.org/security/tags"> 
    <body> 
       <div th:replace="${'portal/' + VIEW} :: content">Content</div> 
    </body> 
</html> 

Controller :

@PreAuthorize("isAuthenticated()") 
  @RequestMapping(value = "/", method = RequestMethod.GET) 
  public String home(Model model) { 
 
    return "portal/home/index"; 
  } 

模板引擎:

public class LayoutTemplateEngine implements ITemplateEngine, MessageSourceAware, InitializingBean { 
 
  private final Logger logger = Logger.getLogger(this.getClass().getName()); 
 
  private final String LAYOUTS_PATH = "layouts/"; 
 
  private final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
 
  @Override 
  public void process(TemplateSpec templateSpec, IContext context, Writer writer) { 
    String template = templateSpec.getTemplate(); 
 
    logger.info("Rendering template: " + template); 
 
    if (context instanceof WebExpressionContext) { 
      int end = template.indexOf("/"); 
      if (end != -1) { 
        // change template 
        templateSpec = new TemplateSpec(LAYOUTS_PATH + template.substring(0, end), templateSpec.getTemplateSelectors(), templateSpec.getTemplateMode(), templateSpec.getTemplateResolutionAttributes()); 
        // add VIEW variable 
        ((WebExpressionContext)context).setVariable("VIEW", template.substring(end + 1)); 
      } 
    } 
 
    templateEngine.process(templateSpec, context, writer); 
    logger.info("Rendering finished"); 
  } 
 
  public void setTemplateResolver(final ITemplateResolver templateResolver) { 
    templateEngine.setTemplateResolver(templateResolver); 
  } 
 
  public void setEnableSpringELCompiler(final boolean enableSpringELCompiler) { 
    templateEngine.setEnableSpringELCompiler(enableSpringELCompiler); 
  } 
 
  public void addDialect(final IDialect dialect) { 
    templateEngine.addDialect(dialect); 
  } 
 
  public void addTemplateResolver(final ITemplateResolver templateResolver) { 
    templateEngine.addTemplateResolver(templateResolver); 
  } 
 
  @Override 
  public IEngineConfiguration getConfiguration() { 
    return templateEngine.getConfiguration(); 
  } 
 
  @Override 
  public String process(String template, IContext context) { 
    return process(new TemplateSpec(template, null, null, null), context); 
  } 
 
  @Override 
  public String process(String template, Set<String> templateSelectors, IContext context) { 
    return process(new TemplateSpec(template, templateSelectors, null, null), context); 
  } 
 
  @SuppressWarnings("resource") 
  @Override 
  public String process(TemplateSpec templateSpec, IContext context) { 
    final Writer stringWriter = new FastStringWriter(100); 
    process(templateSpec, context, stringWriter); 
    return stringWriter.toString(); 
  } 
 
  @Override 
  public void process(String template, IContext context, Writer writer) { 
    process(new TemplateSpec(template, null, null, null), context, writer); 
  } 
 
  @Override 
  public void process(String template, Set<String> templateSelectors, IContext context, Writer writer) { 
    process(new TemplateSpec(template, templateSelectors, null, null), context, writer); 
  } 
 
  @Override 
  public IThrottledTemplateProcessor processThrottled(String template, IContext context) { 
    return processThrottled(new TemplateSpec(template, null, null, null), context); 
  } 
 
  @Override 
  public IThrottledTemplateProcessor processThrottled(String template, Set<String> templateSelectors, IContext context) { 
    return processThrottled(new TemplateSpec(template, templateSelectors, null, null), context); 
  } 
 
  @Override 
  public IThrottledTemplateProcessor processThrottled(TemplateSpec templateSpec, IContext context) { 
    return templateEngine.processThrottled(templateSpec, context); 
  } 
 
  @Override 
  public void afterPropertiesSet() throws Exception { 
    templateEngine.afterPropertiesSet(); 
  } 
 
  @Override 
  public void setMessageSource(MessageSource messageSource) { 
    templateEngine.setMessageSource(messageSource); 
  } 
} 


标签:Spring
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号