首页 文章

使用Thymeleaf页面布局

提问于
浏览
2

我正在尝试在我当前的spring-boot项目中使用百万美元页面布局 . 我将其添加到我的index.html视图(放在resources / templates / public中):

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

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

}

我在这里失踪了什么?

1 回答

  • 0

    该错误只是表明没有指定模板文件位置的 menu.html . 我通常更喜欢XML配置而不是属性文件 . 检查它是否真的应该包含 classpath spring.thymeleaf.prefix 的参考 . 以下是我在 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,意味着你的 class="container" 之夜只是被忽略了 . 替代方案是在包含片段的div中设置类,或者将整个片段封装在外部dif上,然后将其命名为片段 .

相关问题