Blankman
Blankman

Reputation: 266920

Freemarker with spring mvc, so what will my action look like?

In my appname-servlet.xml I have:

<!-- freemarker config -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>

<!-- 

  View resolvers can also be configured with ResourceBundles or XML files. If you need
  different view resolving based on Locale, you have to use the resource bundle resolver.

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

  <!-- if you want to use the Spring FreeMarker macros, set this property to true -->
  <property name="exposeSpringMacroHelpers" value="true"/>

</bean>

So I have my HomeController.java's index view at: /web-inf/freemarker/index.ftl

I am hoping someone can create a dead simple Index action that will create a ModelAndView and use freemarker.

I'm not sure how things will wire together, thanks!

Upvotes: 1

Views: 1978

Answers (2)

Georgy Bolyuba
Georgy Bolyuba

Reputation: 8511

You do not need freemarkerConfig, I think. Just change your view resolver a bit:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value="/WEB-INF/freemarker/"/>
    <property name="suffix" value=".ftl"/>
    <property name="exposeSpringMacroHelpers" value="true"/>
</bean>

Now if you open hppt://localhost:8080/app/index, you will get rendered /WEB-INF/freemarker/index.ftl

Upvotes: 0

skaffman
skaffman

Reputation: 403441

The controllers should have no knowledge of Freemarker, they should just look like any other controller, constructing the ModelAndView or ModelMap as they normally would. The FreeMarkerViewResolver takes the view name held in the ModelAndView and resolves it to a Freemarker Template object internally, rendering your model into that. All freemarker config is internal to the FreeMarkerViewResolver

If your context is not wired up correctly, then the FreeMarkerViewResolver will throw an exception to that effect, but you certainly do not require any freemarker config in your controllers.

Upvotes: 2

Related Questions