简体   繁体   中英

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:

    @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.

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?"

The RequestParam allows you to bind the method parameter argument to the web request parameter. 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. You can optionally set required to false to, well, make the argument optional:

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. 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. 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.

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.

Aside:

@RequestMapping(value="/print") can be more simply written as @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. For example, you have incoming GET request:

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. Because names in request and names of methods args are equals.


But when names are not equals, then you need to specify the correspondence of names explicitly with @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.


Acutally, many developers always use @RequestParam even when names are equal. For example empty @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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