Spring @RequestParam和req.getParameter(" xx")的工作方式不同吗? [英] Does Spring @RequestParam and req.getParameter("xx") work differently..?

查看:309
本文介绍了Spring @RequestParam和req.getParameter(" xx")的工作方式不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请理解我的英语不好。

我使用Spring MVC并更换了此源

I use Spring MVC and I replaced this source

@RequestMapping("/ajax/add_server")
public void addServer(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String host = request.getParameter("host");
    String port = request.getParameter("port");
    String state = request.getParameter("state");

    serverService.addServer(host, port, state);
}

@RequestMapping("/ajax/add_server")
public void addServer(
        @RequestParam("host") String host,
        @RequestParam("port") String port,
        @RequestParam("state") String state) throws Exception {

    serverService.addServer(host, port, state);

}

addServer()方法由AJAX调用。

addServer() Method is called by AJAX.

在使用req.getParameter()的情况下,我的ajax加载图片消失了,但是当我使用@RequestParam时图像没有消失..

My ajax loading image is gone in case of using req.getParameter(), but the images isn't gone when i use @RequestParam..

我猜Ajax XMLRequest对象没有获得任何成功MSG。

I guess Ajax XMLRequest Object doesn't get any success MSG.

但我不知道为什么这是正常的吗?

but I don't know why and is this normal?

额外发现!!

@RequestMapping("/ajax/add_server")
    public void addServer(
            @RequestParam("host") String host,
            @RequestParam("port") String port,
            @RequestParam("state") String state,
            HttpServletResponse response) throws Exception {

        serverService.addServer(host, port, state);
    }

我添加了对Parameter的响应,然后图像消失了。我不知道为什么。

I added response to Parameter then the image's gone. I don't know Why.

我将此留作参考。

具有void返回类型的Controller方法使用基于URI的视图。

The Controller method with void return type uses URI-BASED VIEW.

例如,以下源使用ajax / add_server.jsp作为视图。

For example, this following source uses ajax/add_server.jsp as view.

@RequestMapping("/ajax/add_server")
    public void addServer(
            @RequestParam("host") String host,
            @RequestParam("port") String port,
            @RequestParam("state") String state) throws Exception {

        serverService.addServer(host, port, state);
    }


推荐答案

<$ c的默认值$ c> @RequestParam 是该值是必需的,如果没有,它将抛出异常。另一方面,使用 getParameter ,它只会将null传递给下一个方法。因此,如果有时您没有提供所有三个参数,那么它将无法正常进行更改。

The default for @RequestParam is that the value is required, and it will throw an exception if nothing is there. On the other hand with getParameter it would just be passing a null down into the next method. So if there are some times that you're not supplying all three parameters, then it would not work properly with the change.

编辑:

关于您发布的其他信息:
AnnotationMethodHandlerAdapter中有一些特殊处理,当void方法将HttpServletResponse作为参数接收时,它会更改路由。基本上它假定自从您接收响应后,您正在处理需要生成的任何输出并禁用默认视图分辨率。这将导致服务器简单地回复200并返回空响应体。

Regarding the additional information you posted: There is some special handling inside AnnotationMethodHandlerAdapter that changes the routing when a void method takes in HttpServletResponse as a parameter. Basically it assumes that since you took in the response, you're handling any output that needs to be generated and it disables default view resolution. This would cause the server to simply reply 200 with an empty response body.

如果您有一个void方法,但没有在HttpResponse对象中读取,它正在恢复默认视图分辨率。这可能导致生成错误,因为我怀疑你有一个名为add_server的.jsp文件! :)请求sill工作,因为您的服务调用已完成并在方法返回之前提交并且Spring尝试查看解析。 ajax调用最终会转到错误处理程序而不是成功处理程序。

In the case where you have a void method, but weren't reading in the HttpResponse object, it was reverting to default view resolution. This probably led to an error being generated since I doubt you have a .jsp file named add_server anywhere! :) The request sill "works" since your service call is done and committed before the method returns and Spring attempts view resolution. The ajax call ends up going to the error handler instead of the success handler though.

tl; dr有时带注释的控制器魔法有点太神奇:)

tl;dr sometimes annotated controller "magic" is a little bit too magical :)

这篇关于Spring @RequestParam和req.getParameter(&quot; xx&quot;)的工作方式不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆