rogergl
rogergl

Reputation: 3781

How to configure Spring-MVC to use Groovy Templates?

So far I have configured

<beans>
    <bean id="groovyMarkupConfigurer" class="org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer">
        <property name="resourceLoaderPath" value="classpath:/WEB-INF/templates/" />
    </bean>
</beans>

and

<bean id="groovyMarkupViewResolver"     
      class="org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver"
      p:prefix="/WEB-INF/templates/" p:suffix=".tpl"/>

and I have I file named test.tpl in /WEB-INF/templates/. Than I do the following in my controller:

return "test";

But I keep getting: Could not resolve view with name 'test' in servlet with name 'spring'

BTW: This is a plain Spring MVC project and not a spring boot project.

Upvotes: 2

Views: 1050

Answers (1)

M. Deinum
M. Deinum

Reputation: 125302

There are a couple of things wrong to start with.

  1. /WEB-INF/ isn't part of the classpath. So your current configuration of the GroovyMarkupConfigurer will locate the files in the wrong directory. Remove classpath: from the configuration.

    <property name="resourceLoaderPath" value="/WEB-INF/templates/" />
    
  2. Your current configuration will try to load the classpath/WEB-INF/templates/WEB-INF/templates/test.tpl due to 1 and due to the fact you provided a prefix. Remove the prefix.

    <bean id="groovyMarkupViewResolver" class="org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver" p:suffix=".tpl"/>
    

If you have more UrlBasedViewResolvers then you are in a bit of a pickle as that won't work, they will always return a URL to redirect/forward to for rendering a view.

Upvotes: 3

Related Questions