在控制器响应中使用 VelocityView 或纯文本?

发布于 2024-10-19 16:04:23 字数 1418 浏览 7 评论 0原文

我试图在控制器中返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-10-26 16:04:23

如果你的方法用@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 using ContentNegotiatingViewResolver instead. See the Spring docs for how to set this up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文