0

I am trying to figure out how to pass two different parameters of the same class through the URL to a handler method in the controller. For example, given a "Foo" class with an "id" parameter(I am saying just one parameter to keep it simple, but think from a class with loads of them), the code of the controller looks as it follows:

@Controller(value = "customerCareRemoteService")
public class CustomerCareRemoteServiceImpl {

// Othe methods/requests

    @RequestMapping(value = "/" + "prueba" , method = RequestMethod.GET)
    public @ResponseBody String prueba(Foo pFoo1, Foo pFoo2) {
        //stupid and not interesting code
        String answer = "pFoo1.id is " + pFoo1.id + ". pFoo2.id is " + pFoo2.id.";
        System.out.println(answer);
        return answer;

    }

}

So, when I call this method, there is no way to differ between the two parameters:

http://myfakeurl/prueba?id=1&id=2

How should I deal with that? Is there any way to "prefix" the parameters? I have seen @RequestParam but it does not work for me, because it can not be used with my very own and beauty personal classes(or am I wrong?). Also I would like to avoid wrapper classes. Thank you for your time.

raspayu
  • 5,089
  • 5
  • 37
  • 50

2 Answers2

1

You should use @PathVariable to solve this problem.

Your mapping url would be like this.

@RequestMapping(value = "/" + "prueba" + "/{id1}/{id2}" , method = RequestMethod.GET) 

and the arguements in your function would be.

public @ResponseBody String prueba(@PathVariable int pFoo1, @PathVariable int pFoo2)

In this way you can get both the ID in your controller for further operation using them.

Hope this helped you.

Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44
  • Hi, thanks for the answer, but the point is that I wanted the URL parameter to fill an object attribute. I mean, I have said that the Foo object has one attribute, but it is only to keep the example simple. Actually I am trying to fill various attributes. But thanks :-) – raspayu May 11 '12 at 07:51
  • In that case you should use @RequestAttribute that will bind all your attributes from your view to the model object. – Japan Trivedi May 11 '12 at 11:42
  • Well...I think it works for primitive types, but not for attributes from classes(I mean, not for Foo.id , for example). – raspayu May 11 '12 at 12:20
  • Ohh sorry I meant @ModelAttribute to use. Sorry for mistake. Please check this one. **@ModelAttribute** should do the work for you. :) – Japan Trivedi May 12 '12 at 09:01
  • I am really sorry to be dull, but I have already read that a pair of times while looking on the web, but never found an explanation/example. The documentation from Spring keeps on saying that it is a way to bind the Controller with the model... But I don't understand how to convert parameters in the URL in an Object :-( – raspayu May 14 '12 at 07:27
  • Why you need to convert them? They will be automatically gets bind to your @ModelAttribute object. I'm wondering why you need to convert them. – Japan Trivedi May 14 '12 at 08:08
  • Well...The point is that, I need to be able to pass 2 attributes with the same name of same/different classes as a parameter in the URL. For example: http://fakeurl/prueba?id=1&id=2 . In that case, let's say the first "id" is an attribute from class Param1, and the second "id" is from class Param2. That is what I want to do, rename at least one of them to something,so they dont get mixed(by default, if you say "id=2", then both parameters will get the "2" value. – raspayu May 14 '12 at 09:26
0

So, with some help from a workmate, I have came to the next page that talks about that thema:

Customizing Parameter Names When Binding Spring MVC Command Objects

Which leads to the next StackOverFlow question:

How to customize parameter names when binding spring mvc command objects

So it looks like there is no easy solution available like "using annotation X", but to create your own resolver, and use custom annotations (as a good newbie, I am still trying to understand it :-S).

Community
  • 1
  • 1
raspayu
  • 5,089
  • 5
  • 37
  • 50