Thymeleaf 3和Tiles2集成 [英] Thymeleaf 3 and Tiles2 integration

查看:165
本文介绍了Thymeleaf 3和Tiles2集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does Thymeleaf 3 support Tiles 2 somehow? There is a package that I was using for Thumeleaf 2.x.x thymeleaf-extras-tiles2-spring4 but as I see now it is not compatible because of changes in org.thymeleaf.dialect.AbstractDialect class

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开始?

Do I need to wait for an update of this integration to be able to start with T3?

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

Is there any way that I can simulate Tiles in Thymeleaf3

我仅将Tiles用于类似这样的事情:

I only use my Tiles for something like this:

  <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()方法.

To solve that problem I have created a proxy for SpringTemplateEngine and advisedTemplateEngine.process() method.

工作方式:

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

The code map layouts on the basis of template path for instance:

portal/moje_konto/moje_dane

已映射到布局

 LAYOUTS_PATH/portal

此外,它还会传递变量VIEW,其中包含指向实际模板的路径

Additionally, it passes variable VIEW containing a path to actual template

在内部布局中,它可以用来包含特定的片段. 非常简单的门户布局可能看起来像这样:

Inside layouts it may be used to include specific fragments. Very simple portal layout may look like that:

<!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>

控制器:

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

    return "portal/home/index";
  }

TemplateEngine:

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

这篇关于Thymeleaf 3和Tiles2集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆