首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thymeleaf 3与Tiles2集成

Thymeleaf 3与Tiles2集成
EN

Stack Overflow用户
提问于 2016-05-19 12:06:31
回答 1查看 775关注 0票数 3

Thymeleaf 3以某种方式支持瓷砖2吗?有一个包是我为Thumeleaf2.x.x thymeleaf-extras-tiles2-spring4使用的,但是我现在看到它不兼容,因为org.thymeleaf.dialect.AbstractDialect类中的变化

代码语言:javascript
复制
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中模拟瓷砖?

我只用瓷砖做这样的事情:

代码语言:javascript
复制
  <definition name="portal/**" template="layouts/portal">
    <put-attribute name="_head" value="/portal/{1} :: _head"/>
    <put-attribute name="content" value="/portal/{1} :: content"/>
  </definition>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-10 13:44:31

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

的工作方式:

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

代码语言:javascript
复制
portal/moje_konto/moje_dane

映射到布局。

代码语言:javascript
复制
 LAYOUTS_PATH/portal

此外,它还传递包含实际模板路径的变量视图。

在布局中,可以使用它来包含特定的片段。非常简单的门户布局可能如下所示:

代码语言:javascript
复制
<!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>

控制器:

代码语言:javascript
复制
@PreAuthorize("isAuthenticated()")
  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home(Model model) {

    return "portal/home/index";
  }

TemplateEngine:

代码语言:javascript
复制
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);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37322740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档