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
In your view resolver you can add the following property
Then in your freemarker template you can get the request context patch like
If your requirement is to fetch the Context Path in your FTL view then Spring provides a better alternate - First import spring.ftl in your view
Then use macro @spring.url for the URL which you want to make context aware -
This is very much similar to -
Where rc is defined in viewResolver
XML based configuration
or Spring Boot style configuration (aplication.yml)