有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC3+Apache tiles+Thymeleaf集成问题

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

我正在尝试打开登录。扩展布局定义的html

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

请给我建议解决方案

下面是我的代码片段

网络。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> 

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

布局。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>

登录。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之后,也添加了我的项目结构。。 我应该移动我的瓷砖吗。还有xml文件吗

Project Structure


共 (1) 个答案

  1. # 1 楼答案

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