我使用具有以下结构的Thymeleaf和Tiles的Spring进行了一个项目:
我已经配置了我的Spring-Servlet:
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="characterEncoding" value="UTF-8" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="tilesConfigurer"
class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="viewClass"
value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView" />
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
</set>
</property>
</bean>
我的Tiles定义是:
<tiles-definitions>
<definition name="layout" template="layout">
<put-attribute name="header" value="header" />
<put-attribute name="menu" value="menu" />
<put-attribute name="footer" value="footer" />
<put-attribute name="body" />
</definition>
<definition name="usersView" extends="layout">
<put-attribute name="body" value="pages :: users" />
</definition>
</tiles-definitions>
这会给我以下错误:
Error resolving template "pages", template might not exist or might not be accessible by any of the configured Template Resolvers
但是当我将users.html页面移动到templates文件夹并将我的tile def设为
时<definition name="usersView" extends="layout">
<put-attribute name="body" value="users" />
</definition>
工作正常。
所以问题是Thymeleaf可以接受我的文件夹结构吗?如果是,怎么做?
答案 0 :(得分:3)
是的,可以使用 viewNames属性
只需将Spring-Servlet.xml更改为
即可<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
<property name="characterEncoding" value="UTF-8" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="tilesConfigurer"
class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="viewClass"
value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView" />
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="1" />
<property name="viewNames" value="templates/*,pages/*" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
</set>
</property>
</bean>
并将您的图块定义为
<tiles-definitions>
<definition name="layout" template="templates/layout">
<put-attribute name="header" value="templates/header" />
<put-attribute name="menu" value="templates/menu" />
<put-attribute name="footer" value="templates/footer" />
<put-attribute name="body" />
</definition>
<definition name="pages/usersView" extends="layout">
<put-attribute name="body" value="pages/users" />
</definition>
</tiles-definitions>
说明:
我们在Resolver的前缀中定义了根文件夹
<property name="prefix" value="/WEB-INF/" />
并通过
进行扩展 <property name="viewNames" value="templates/*,pages/*" />
到您需要的文件夹和您创建的任何新视图文件夹都应该在此属性中也重定向或转发
答案 1 :(得分:0)
并确保用Repo图层编写的查询可能不正确。如果是这样,一旦满足条件,请检查您的查询(使用CriteriaBuilder,CriteriaQuery,Predicate)。
在我的情况下,我试图从不满足条件的数据库中获取值。