Spring框架Freemarker宏布局,嵌套不起作用

时间:2016-04-30 11:02:57

标签: java spring layout freemarker

我为我的网络应用程序使用spring框架 我在目录web / WEB-INF / views / layout

中有主要的布局application.ftl
<#macro layout>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        Some content here
    <#nested>
        Some content here
    </body>
    </html></#macro>

在目录web / WEB-INF / views中有另一个页面, 例如page1.ftl

<@layout>This is the page one</@layout>

和第2页ftl。

<@layout>This is the page two</@layout>

但它不起作用。我的dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <import resource="spring/spring-aop.xml" />
    <context:component-scan base-package="com.birthright.controllers"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="WEB-INF/resources/"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
          p:cache="true"
          p:prefix=""
          p:suffix=".ftl"
          p:contentType="text/html; charset=UTF-8"/>

    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"
          p:templateLoaderPath="/WEB-INF/views/"
          p:defaultEncoding="UTF-8">
        <property name="templateLoaderPaths">
            <list>
                <value>/WEB-INF/views/layout</value>
            </list>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="datetime_format">dd-MM-yyyy HH:mm:ss</prop>
                <prop key="number_format">0.######</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
            </props>
        </property>
    </bean>
</beans>

1 个答案:

答案 0 :(得分:0)

您是否尝试将宏文件导入视图? <#import "./layout/application.ftl">

此外,如果您在WEB-INF / views /目录中有视图,则templateLoaderPaths应指向该目录,而不是WEB-INF / views / layout。您的配置没有指向正确的目录,因此您的查看无法找到。 <value>/WEB-INF/views</value>