Spring MVC, Tiles 3 and Freemarker Integration

I included Freemarker in an existing spring MVC and Tiles 3 application and found it was unable to read the fragment definition file. It directly reads the content tile, which is one of the three tiles configured in the tile definition file, bypassing the tile definition file. How can I get it to read the tile definition file? Here are my codes:

applicationContext.xml

<mvc:annotation-driven />

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tile-defs/view.xml</value>
            <value>/WEB-INF/tile-defs/survey.xml</value>
        </list>
    </property>
</bean>

<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="freemarkerVariables">
        <map>
            <entry key="xml_escape" value-ref="fmXmlEscape"/>
        </map>
    </property>
    <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">3</prop>
        </props>
    </property>
</bean>

<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
    <property name="exposeSpringMacroHelpers" value="true"/>
    <property name="exposeRequestAttributes" value="true"/>
    <property name="exposeSessionAttributes" value="true"/>
</bean>

      

<strong> tiles-def.xml

<tiles-definitions>
<definition name="template"
    template="/WEB-INF/views/main_template.ftl">
    <put-attribute name="header"
        value="/WEB-INF/views/tiles/header.ftl" />
    <put-attribute name="footer"
        value="/WEB-INF/views/tiles/footer.ftl" />
</definition>
<definition name="home" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/home.ftl" />
</definition>

      

main_template.ftl

<body>

    <!-- Header -->
    <tiles:insertAttribute name="header" />

    <tiles:insertAttribute name="content" />

    <!-- Footer Page -->
    <tiles:insertAttribute name="footer" />

</body>

      

home.ftl

<#import "spring.ftl" as spring />



Hello world!  


<P>  The time on the server is ${serverTime}. </P>
<P>  ${message}. </P>

      

header.ftl

<#import "spring.ftl" as spring />

This is header

      

+3


source to share


1 answer


I had the same problem. I solved it by adding this to the top of my ftl files.

<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

and using



<@tiles.insertAttribute name="header" />

      

to insert something from the tile definition.

0


source







All Articles