当前位置: 首页 > 知识库问答 >
问题:

模板可能不存在或不可访问

汪晨
2023-03-14

我一直在尝试在SpringMVC中使用thymeleaf布局方言,如中所述https://github.com/ultraq/thymeleaf-layout-dialect.我的springservlet如下所示

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

<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  <beans:property name="templateResolver" ref="templateResolver" />
  <!-- These lines add the dialect to Thymeleaf -->
  <beans:property name="additionalDialects">
    <beans:set>
      <beans:bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
    </beans:set>
  </beans:property>
</beans:bean>

<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
  <beans:property name="templateEngine" ref="templateEngine" />
  <beans:property name="characterEncoding" value="UTF-8" />
</beans:bean>

我已将所有模板文件保存在/WEB-INF/templates/中。当我在thymeleaf中使用以下代码创建和使用诱惑时

<html lang="en" xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorator="Layout.html">

我发现以下错误:

[THYMELEAF] * Dialect [1 of 2]: org.thymeleaf.spring3.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] * Dialect [2 of 2]: nz.net.ultraq.thymeleaf.LayoutDialect
[THYMELEAF]     * Prefix: "layout"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
19:32:40,992 INFO  [org.thymeleaf.TemplateEngine] (http-localhost-127.0.0.1-8080-1) [THYMELEAF] TEMPLATE ENGINE INITIALIZED
19:32:41,199 ERROR [org.thymeleaf.TemplateEngine] (http-localhost-127.0.0.1-8080-1) [THYMELEAF][http-localhost-127.0.0.1-8080-1] Exception processing template "home": Error resolving template "Layout.html", template might not exist or might not be accessible by any of the configured Template Resolvers (home:4)
19:32:41,202 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/expensemanager].[appServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet appServlet threw exception: org.thymeleaf.exceptions.TemplateInputException: Error resolving template "MainLayout.html", template might not exist or might not be accessible by any of the configured Template Resolvers (home:4)

如果我不布局:decorator=“layout.html”代码可以完美地工作,没有任何错误。

下图显示了我的项目结构

共有1个答案

邵捷
2023-03-14

我通常这样做:

<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
  <beans:property name="characterEncoding" value="UTF-8" />
  <beans:property name="templateEngine">
    <beans:bean class="org.thymeleaf.spring3.SpringTemplateEngine">
      <beans:property name="additionalDialects">
        <beans:set>
          <beans:bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
        </beans:set>
      </beans:property>
      <beans:property name="templateResolvers">
        <beans:bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
          <beans:property name="prefix" value="/WEB-INF/templates/" />
          <beans:property name="suffix" value=".html" />
          <beans:property name="templateMode" value="HTML5" />
        </beans:bean>
      </beans:property>
    </beans:bean>
  </beans:property>
</beans:bean>

如果这不起作用,你能发布文件和文件夹结构的截图吗?

 类似资料: