I’m trying to return content other than json in my Controller, but I can’t seem to get it to work. Ideally, I’d like to return a rendered velocity template as plain text or html.
This is what I have in my controller:
@RequestMapping( value = "time", headers = "Accept=*/*", method = RequestMethod.GET )
public @ResponseBody
Date getCurrentTime( HttpServletRequest request, HttpServletResponse response ) {
response.setContentType( "text/plain" );
return new Date();
}
And this is in my springmvc-servlet.xml (I know this is not right…but I’m a bit lost here):
<context:component-scan base-package="com.paml.alerter.controller" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
</bean>
<!-- This bean sets up the Velocity environment for us based on a root path
for templates. Optionally, a properties file can be specified for more control
over the Velocity environment, but the defaults are pretty sane for file
based template loading. -->
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</bean>
Does anyone know how to set this up right?
TIA
If your method is annotated with
@ResponseBody, then the Spring MVC view layer will be bypassed entirely.If you’re not interested in JSON output, then
@ResponseBodyis inappropriate – just remove it, and your Velocity views will be used.If you need to switch between JSON and some other
Viewlayer, then you should consider removing@ResponseBodyand usingContentNegotiatingViewResolverinstead. See the Spring docs for how to set this up.