Spring MVC 请求没有渲染视图

发布于 2024-10-24 01:31:38 字数 1866 浏览 2 评论 0原文

我在使用 Spring 配置来实现以下功能时遇到问题 - 我需要视图请求正常运行(渲染视图),并且服务请求只需执行而不渲染任何内容。 我遇到的问题是,在控制器执行之后,Spring 决定应该实例化 ModelAndView,即使控制器方法不返回任何内容(void)。这会触发一个视图被渲染,而实际上我只想在控制器完成其工作后什么也不做。 我确信这一定是我在 Spring 配置中做错了(我的猜测是它与视图解析器有关)。 对此的任何帮助表示赞赏。 谢谢。

代码如下:

@Controller
@RequestMapping( "actions" )
public final class ServiceController{
    private static final Logger logger = LoggerFactory.getLogger( ServiceController.class );
    @RequestMapping( value = "/submit.service",method = RequestMethod.POST )
    public void test( @RequestParam( "mail" ) String mail ){
        ServiceController.logger.info( mail );
    }
}

web.xml 中的 servlet:

<servlet>
    <servlet-name>viewServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/viewServlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>viewServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

上下文是:

<annotation-driven />
<beans:bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <beans:property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</beans:bean>

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <beans:property name="cache" value="true" />
    <beans:property name="prefix" value="" />
    <beans:property name="suffix" value=".vm" />
</beans:bean>

I am having trouble with a Spring configuration for the following functionality - I need the view requests to behave normally (render the view) and the service requests to simply execute without rendering anything.
The problem I'm having is that, after the controller executes, somewhere down the line Spring decides that a ModelAndView should get instantiated even though the controller method doesn't return anything (void). This triggers a view to be rendered, when in fact I want to simply do nothing once the controller has done it's job.
I'm sure it must be something I'm doing wrong in the Spring configuration (my guess is that it's related to the view resolver).
Any help on this is appreciated.
Thanks.

Here is the code:

@Controller
@RequestMapping( "actions" )
public final class ServiceController{
    private static final Logger logger = LoggerFactory.getLogger( ServiceController.class );
    @RequestMapping( value = "/submit.service",method = RequestMethod.POST )
    public void test( @RequestParam( "mail" ) String mail ){
        ServiceController.logger.info( mail );
    }
}

And the servlets in web.xml:

<servlet>
    <servlet-name>viewServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/viewServlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>viewServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The context is:

<annotation-driven />
<beans:bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <beans:property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</beans:bean>

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <beans:property name="cache" value="true" />
    <beans:property name="prefix" value="" />
    <beans:property name="suffix" value=".vm" />
</beans:bean>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凉城已无爱 2024-10-31 01:31:38

返回 null 不起作用。

与此同时,我找到了解决方案 - 我需要用

@ResponseBody注释该方法

Returning null did not work.

In the meantime, I found the solution - I needed to annotate the method with

@ResponseBody

我恋#小黄人 2024-10-31 01:31:38

另一种解决方案是返回一个responseEntity,http://www. Captaindebug.com/2011/07/using-spring-responsentity-class.html,毕竟你需要一种方法来向客户端发出错误信号,并且如果发生异常,则使用 @ResponseEntity 来渲染它一个 html 页面。使用 ResponseEntity,您可以使用 http 500 添加自定义数据进行响应。

another solution would be to return a responseEntity, http://www.captaindebug.com/2011/07/using-spring-responseentity-class.html, after all you will need a way to signal an error back to the client, and with @ResponseEntity if an exception occurs spring renders this in a html page. with ResponseEntity you can respond with http 500 add custom data.

久光 2024-10-31 01:31:38

我相信您可以通过返回 null 来实现这一点。

I believe you can achieve that, by returning null.

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