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

时间:2014-09-06 14:11:31

标签: spring-mvc thymeleaf

我一直在尝试使用https://github.com/ultraq/thymeleaf-layout-dialect中提到的带有spring mvc的百万美元布局方言。我的spring-servlet如下所示

<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 /中。当我使用以下代码在百里香中创建和使用诱惑

<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 =&#34; Layout.html&#34;代码完美无误地运行。

下面是显示我的项目结构的图像

enter image description here

1 个答案:

答案 0 :(得分:0)

我通常这样做:

<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>

如果这不起作用,您可以发布文件和文件夹结构的屏幕截图吗?