简体   繁体   English

Spring MVC3 + Apache tile + Thymeleaf集成问题

[英]Spring MVC3 + Apache tiles + Thymeleaf Integration Issue

I am facing below error while integrating Spring MVC 3 + Thymeleaf + Apache Tiles. 在集成Spring MVC 3 + Thymeleaf + Apache Tiles时,我面临以下错误。

I am trying to open a login.html which extends layout definitions. 我正在尝试打开login.html来扩展布局定义。

Error resolving template "/WEB-INF/templates/layout.html", template might not exist or might not be accessible by any of the configured Template Resolvers 解析模板“ /WEB-INF/templates/layout.html”时出错,该模板可能不存在,或者任何配置的模板解析器都无法访问该模板

Please suggest me the solution 请给我建议解决方案

Below is my code snippet 以下是我的代码段

web.xml 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 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 的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>

tiles-def.xml 瓷砖,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 的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>

Adding my project structure as well after moving from web-inf to webapps.. Should i move my tiles-def.xml file as well ? 从web-inf移到webapps之后,也要添加我的项目结构。我也应该移动我的tile-def.xml文件吗?

Project Structure 项目结构

Please move the jsp and html pages from the web-inf folder to another folder like "webapp". 请将jsp和html页面从web-inf文件夹移动到另一个文件夹,例如“ webapp”。 If they are in the web-inf folder you can not access them. 如果它们在web-inf文件夹中,则无法访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM