How to get the request context in a freemaker template in spring

Arun P Johny picture Arun P Johny · Aug 8, 2009 · Viewed 17.9k times · Source

How to get the request context path in freemarker template when using with spring?

My view resolver is like this

    <bean id="freeMarkerViewResolver" class="learn.common.web.view.FreemarkerViewResolver">
        <property name="order" value="1" />
        <property name="viewClass"
        value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="suffix" value=".ftl" />
        <property name="cache" value="false" />
    </bean>

My view resolver learn.common.web.view.FreemarkerViewResolver extends org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver

Answer

Arun P Johny picture Arun P Johny · Aug 8, 2009

In your view resolver you can add the following property

<property name="requestContextAttribute" value="rc"/>

Then in your freemarker template you can get the request context patch like

${rc.getContextPath()}