栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Thymeleaf 3和Tiles2集成

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Thymeleaf 3和Tiles2集成

为了解决该问题,我创建了一个代理

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>

控制器:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382801.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号