【问题标题】:Working with Thymeleaf Page Layouts使用 Thymeleaf 页面布局
【发布时间】:2015-03-16 12:21:46
【问题描述】:

我正在尝试在我当前的 spring-boot 项目中使用 thymeleaf 页面布局。我将此添加到我的 index.html 视图中(放置在资源/模板/公共中):

<nav class="navbar navbar-default navbar-fixed-top">
  <div th:include="menu :: menu">...</div>
</nav>

视图 menu.html 是这样的:

<!DOCTYPE html>
<html>
<head></head>
<body>

<div class="container" th:fragment="menu">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" th:href="@{/}">Project name</a>
  </div>
  <div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">

    </ul>
    <ul class="nav navbar-nav navbar-right">
      <li sec:authorize="isAnonymous()"><a th:href="@{/signin}">Entrar</a></li>
      <li sec:authorize="isAnonymous()"><a th:href="@{/signup}">Cadastro</a></li>
      <li sec:authorize="isAuthenticated()"><a th:href="@{/logout}">Logout</a></li>
    </ul>
  </div><!--/.nav-collapse -->
</div>

</body>
</html>

但是当我运行项目时,我收到了这个错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "menu", template might not exist or might not be accessible by any of the configured Template Resolvers (public/index:77)] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "menu", template might not exist or might not be accessible by any of the configured Template Resolvers (public/index:77)

我的 application.properties 文件中有 thymeleaf 的此配置:

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

我也有这个配置类:

@Configuration
public class ThymeleafContext {

  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine  =  new SpringTemplateEngine();

    engine.addTemplateResolver(templateResolver());

    final Set<IDialect> dialects = new HashSet<IDialect>();
    dialects.add( new SpringSecurityDialect() );
    engine.setDialects( dialects );

    return engine;
  }

  public ITemplateResolver templateResolver() {
    TemplateResolver resolver = new TemplateResolver();
    return resolver;
  }

}

我在这里缺少什么?

【问题讨论】:

  • 您的 menu.html 与 index.html 位于同一文件夹中还是在子文件夹中?在后者中,您需要将子文件夹添加到 th:include。喜欢子文件夹/菜单::菜单
  • @MystyxMac 它在同一个文件夹中。
  • 即使它们在同一个文件夹中,我认为 Thymeleaf 从前缀开始搜索(在本例中为 /templates/,而 menu.html 在 //templates/public。你可以试试公共/菜单 :: 菜单?

标签: spring layout spring-boot thymeleaf


【解决方案1】:

错误只是表明没有menu.html 您在其中指定了模板文件的位置。我通常更喜欢 XML 配置而不是属性文件。检查它是否真的应该在spring.thymeleaf.prefix 上包含classpath 的引用。下面是我在mvc-dispatcher.xml 上使用的配置来指定 Thymeleaf 参数。据我所知,您的配置是相似的,只是不是 XML:

<bean id="templateResolver"  class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="cacheable" value="false" />
</bean>

如您所见,我指的不是classpath,而是我的WEB-INF 文件夹。尝试更改为 XML 并确保您的 menu.html 不在子文件夹中。

OBS:注意在片段上指定一个 css 类,Thymeleaf 可能不会加载它,因为它加载的是片段内部的内容,而不是被引用的实际 div,这意味着你的 @ 987654329@晚上干脆无视。替代方法是在包含片段的 div 中设置类,或者将整个片段封装在外部 div 上,然后将其命名为片段。

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 2016-07-17
    • 2014-06-06
    • 2020-08-23
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多