Announcement Announcement Module
Collapse
No announcement yet.
@RequestParam with Lists Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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, 03:41 PM.

  • #2
    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){
    
    }

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


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

          Comment

          Working...
          X