Spring MVC3 + Apache tiles + Thymeleaf Integration问题

时间:2015-11-05 04:04:38

标签: java spring-mvc thymeleaf apache-tiles

在集成Spring MVC 3 + Thymeleaf + Apache Tiles时,我面临以下错误。

我正在尝试打开一个扩展布局定义的login.html。

解析模板" /WEB-INF/templates/layout.html"时出错,模板可能不存在,或者任何配置的模板解析器都无法访问

请建议我解决方案

以下是我的代码段

的web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Thymeleaf Tiles Example</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app> 

disptacher- servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.bosch.chandan.springpoc.controller" />

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />


    <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" />
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="additionalDialects">
            <set>
                <bean
                    class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect" />
                <bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
            </set>
        </property>
    </bean>

    <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
        <property name="viewClass" value="org.thymeleaf.extras.tiles2.spring3.web.view.ThymeleafTilesView"/>
        <property name="templateEngine" ref="templateEngine" />
        <property name="order" value="1"></property>
    </bean>

    <bean id="tilesConfigurer"
        class="org.thymeleaf.extras.tiles2.spring3.web.configurer.ThymeleafTilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-defs.xml</value>
            </list>
        </property>
    </bean>
</beans>

的layout.html

<!DOCTYPE html>
<html xmlns:tiles="http://www.thymeleaf.org">
<head>          

</head> 
<body>  
        <div tiles:include="header">Header Block</div>      
        <div tiles:substituteby="body">Body Block</div>         
        <div tiles:substituteby="footer">Footer Block</div>

</body>
</html>

瓦片-def.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC
          "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
          "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>

    <definition name="layout" template="/WEB-INF/templates/layout.html">
        <put-attribute name="header" value="/WEB-INF/templates/header.html"></put-attribute>
        <put-attribute name="body" value="/WEB-INF/templates/Mainhome.html"></put-attribute>
        <put-attribute name="footer" value="/WEB-INF/templates/footer.html"></put-attribute>
    </definition>

    <definition name="login" extends="layout">
    </definition>
</tiles-definitions>

的login.html

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">


<body bgcolor="#66FF99">

    <form action="" method="post">

        <fieldset>
            <legend>Enter Login Details</legend>
        </fieldset>
    </form>
</body>
</html>

从web-inf迁移到webapps后,添加我的项目结构.. 我应该移动我的tiles-def.xml文件吗?

Project Structure

1 个答案:

答案 0 :(得分:0)

请将jsp和html页面从web-inf文件夹移动到另一个文件夹,如“webapp”。如果它们位于web-inf文件夹中,则无法访问它们。