为Thymeleaf 3模板解析器使用多个文件夹时出现FileNotFoundException

时间:2015-11-04 15:57:13

标签: java thymeleaf

我正在寻求解决位于不同文件夹中的Thymelaf模板。例如,我想将电子邮件模板放在名为WEB-INF/thymeleaf/email的文件夹和上面一层的网页模板中。

这适用于电子邮件模板,但请求WEB-INF/thymeleaf下的任何模板会产生FileNotFoundException

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/thymeleaf/email/WebFile.html] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) at org.thymeleaf.spring4.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103)

以下是我的尝试:

  <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="order" value="1" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="templateEngine" ref="templateEngine" />
  </bean>

  <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolvers">
      <set>
    <ref bean="emailTemplateResolver" />
    <ref bean="webTemplateResolver" />
      </set>
    </property>
    <property name="additionalDialects">
      <set>
        <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
      </set>
    </property>
  </bean>

  <bean id="emailTemplateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/thymeleaf/email/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="cacheable" value="false"/>
    <property name="order" value="2" />
  </bean>

  <bean id="webTemplateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/thymeleaf/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="cacheable" value="false" />
    <property name="order" value="3" />
  </bean>

注意:我正在使用新的Thymeleaf 3.0.0.BETA01来利用更宽松的解析功能。我的堆栈跟踪反映了新库,因此我知道它已正确部署。我使用SpringResourceTemplateResolver基于migration guide

即使是变通方法也会受到赞赏(除了将所有文件放在一个文件夹中)。

1 个答案:

答案 0 :(得分:0)

我能够通过不明确说明电子邮件文件夹来实现这一目的:

<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="order" value="1"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateEngine" ref="templateEngine"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolvers">
        <set>
            <ref bean="webTemplateResolver"/>
        </set>
    </property>
    <property name="additionalDialects">
        <set>
            <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
        </set>
    </property>
</bean>

<bean id="webTemplateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/thymeleaf/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="cacheable" value="false"/>
    <property name="order" value="3"/>
</bean>

(我已经将Spring Security升级到版本4,但我不相信这是相关的。)

我的结构如下:

WEB-INF
  --->thymeleaf
      page.html
          --->email
              emailTemplate.html