在控制器响应中使用 VelocityView 或纯文本?
我试图在控制器中返回 json 以外的内容,但我似乎无法让它工作。理想情况下,我想以纯文本或 html 形式返回渲染的速度模板。
这是我在控制器中的内容:
@RequestMapping( value = "time", headers = "Accept=*/*", method = RequestMethod.GET )
public @ResponseBody
Date getCurrentTime( HttpServletRequest request, HttpServletResponse response ) {
response.setContentType( "text/plain" );
return new Date();
}
这是在我的 springmvc-servlet.xml 中(我知道这是不对的......但我在这里有点迷失):
<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>
有谁知道如何正确设置它?
TIA
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你的方法用@ResponseBody注释,那么Spring MVC视图层将被完全绕过。
如果您对 JSON 输出不感兴趣,那么
@ResponseBody
是不合适的 - 只需将其删除,您的 Velocity 视图就会被使用。如果您需要在 JSON 和其他
View
层之间切换,那么您应该考虑删除@ResponseBody
并使用ContentNegotiatingViewResolver
代替。请参阅 Spring docs 了解如何设置。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
@ResponseBody
is inappropriate - just remove it, and your Velocity views will be used.If you need to switch between JSON and some other
View
layer, then you should consider removing@ResponseBody
and usingContentNegotiatingViewResolver
instead. See the Spring docs for how to set this up.