简体   繁体   English

将值传递给Spring MVC中的控制器,有什么区别?

[英]Pass value to controller in Spring MVC, what is the difference?

When I read some codes written with Spring MVC in an old project, I notice that the author using two similar method to pass value to controller as follows: 当我在一个旧项目中阅读一些用Spring MVC编写的代码时,我注意到作者使用两种类似的方法将值传递给控制器​​,如下所示:

    @RequestMapping(value="/print")
    public String PrintInfo(@RequestParam("name") String name,@RequestParam("age") int age) {
.......
}

Or 要么

    @RequestMapping(value="/print")
    public String PrintInfo(String name,int age) {
.......
}

Both them work well. 他们俩都运作良好。 The difference is whether it use the @RequestParam. 区别在于是否使用@RequestParam。

So what's the main difference between them? 那么它们之间的主要区别是什么? Which one is better, and why? 哪一个更好,为什么?

This basically sounds to me like you're asking "what is a RequestParam and why should I use it?" 基本上,这听起来像是您在问“什么是RequestParam ,为什么要使用它?”

The RequestParam allows you to bind the method parameter argument to the web request parameter. RequestParam允许您将方法参数参数绑定到Web请求参数。 Without any other attributes, your example tells Spring that a name and age parameter are required and Spring will know to associate those two parameters against the incoming request. 如果没有其他任何属性,您的示例将告诉Spring要求提供nameage参数,并且Spring将知道将这两个参数与传入的请求相关联。 You can optionally set required to false to, well, make the argument optional: 您可以选择将required设置为false以使参数成为可选参数:

public String PrintInfo(@RequestParam("name", required = false) String name,
                        @RequestParam("age") int age) {

As an extremely useful feature, you can also provide a defaultValue in case you receive an empty value from the request. 作为一项非常有用的功能,如果您从请求中收到一个空值,还可以提供defaultValue So you can do: 因此,您可以执行以下操作:

public String PrintInfo(@RequestParam("name", defaultValue="John Doe") String name,
                        @RequestParam("age") int age) {

...and you'll never deal with a null name. ...而且您将永远不会使用空名称。

Finally, using it also does some magic type conversions, like for example automatically using an Integer type. 最后,使用它还会执行一些魔术类型转换,例如自动使用Integer类型。 In your example, you could have used: 在您的示例中,您可能使用了:

public String PrintInfo(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {

...and Spring would have boxed it automatically without you doing any extra work. ...而Spring会自动将其装箱,无需您进行任何额外的工作。

There's nothing inherently wrong with leaving off the RequestParam annotation, but you're essentially saying no to Spring enabling these features as you have in your second case. 放弃RequestParam批注并没有本质上的错误,但是实际上,您拒绝对Spring启用这些功能(就像在第二种情况下一样)表示不接受。

Aside: 在旁边:

@RequestMapping(value="/print") can be more simply written as @RequestMapping("/print") @RequestMapping(value="/print")可以更简单地写为@RequestMapping("/print")

If the name of request parameter and the name of method arguments will be equal, then Spring will bind parameters automatically by names. 如果请求参数名称和方法参数名称相等,则Spring将按名称自动绑定参数。 For example, you have incoming GET request: 例如,您有传入的GET请求:

http://localhost:8080/print?name=somename&age=30

And controller method: 和控制器方法:

@RequestMapping(value="/print")
public String PrintInfo(String name,int age) {
    ...
}

In this case you don't need to specify @RequestParam annotation for parameter. 在这种情况下,您不需要为参数指定@RequestParam批注。 Because names in request and names of methods args are equals. 因为请求中的名称与方法args的名称相等。


But when names are not equals, then you need to specify the correspondence of names explicitly with @RequestParam . 但是,当名称不相等时,则需要使用@RequestParam显式指定名称的对应关系。 For example: 例如:

http://localhost:8080/print?user_name=somename&user_age=30

And controller method: 和控制器方法:

@RequestMapping(value="/print")
public String PrintInfo(@RequestParam("user_name") String userName, @RequestParam("user_age")int userAge) {
    ...
}

So @RequestParam needed to help the Spring make bindings properly, when request param names and method args names are different. 因此,当请求参数名称和方法args名称不同时,@ @RequestParam需要帮助Spring正确进行绑定。


Acutally, many developers always use @RequestParam even when names are equal. 因此,即使名称相同,许多开发人员也始终使用@RequestParam For example empty @RequestParam : 例如空的@RequestParam

@RequestMapping(value="/print")
public String PrintInfo(@RequestParam() String name, @RequestParam() int age) {
    ...
}

Because this annotation shows that argument comes from request and makes your code more clear and readable. 因为此批注显示参数来自请求,并使您的代码更清晰易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM