views:

41

answers:

2

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!

+2  A: 

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.

skaffman
ok that's pretty slick, thanks.
Blankman
A: 

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

Georgy Bolyuba