Results 1 to 5 of 5

Thread: @RequestParam with Lists

  1. #1
    Join Date
    Jun 2008
    Posts
    3

    Default @RequestParam with Lists

    I'm trying to bind a series of parameters to a List or Array (order matters) and I'm using the RequestParam annotation. How do I go about passing the parameters.

    Here is my sample controller method (I could not use the AT symbol in the post because of post restrictions so I replace it with <at>):
    Code:
    <at>RequestMapping
    public void test(<at>RequestParam("constraints") List<String> constraints) {
        for (String constraint : constraints) {
            System.out.println(constraint);
        }
    }
    Is this possible?
    How do I form the URL?
    I tried .../test.jhtml?constraints=test1&constraints=test2 this works, but I can't find documentation indicating if the order of values is preserved or not.
    I tried .../test.jhtml?constraints[0]=test1&constraints[1]=test2 but I get an error saying that the constraints parameter is required.

    Note: I looked into StringArrayPropertyEditor but I would have to create a custom implementation because I can't use a set of characters as delimiters because the input is not restricted to a set of characters.

    Any ideas on how to do this?
    Last edited by hookumsnivy; Jun 13th, 2008 at 02:41 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    21

    Default

    When you initially create your page try passing it a List as a model attribute and bind the individual elements of the page to that list then you can retrieve it by using

    Code:
    public void test(@ModelAttribute("constraints") List<String> constraints){
    
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    3

    Default

    Thanks Ben_Leedham

    That didn't work, but it pointed me in the right direction.
    I was unable to find a way to bind a list directly:

    Code:
    public void test(@ModelAttribute("constraints") ArrayList<String> constraints){
    btw, you can't use just List because Spring will complain that it can't initialize it.

    but it works if I put a wrapper around it:
    Code:
    public void test(@ModelAttribute("constraints") Contraints constraints){
    ...
    }
        public static class Constraints {
            private List<String> items = new ArrayList<String>();
    
            public List<String> getItems() {
                return items;
            }
    
            public void setItems(List<String> items) {
                this.items = items;
            }
        }
    The problem I found is that a List doesn't have a property that can be set. So the request parameter names aren't clear. When I use the Constraints object, the parameters become:
    items[0], items[1], etc

  4. #4
    Join Date
    Jun 2008
    Posts
    21

    Default

    Glad it helped. I dint get chance to check the List advice before supplying it, so apologies if it wasn't totallly correct, but glad you sorted your problem out.

  5. #5
    Join Date
    Apr 2013
    Posts
    2

    Default

    I hate the fact that I have to wrap my lists around a wrapper object. A lamentable overkill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •