Announcement Announcement Module
Collapse
No announcement yet.
Binding complex type to annotated controller action Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding complex type to annotated controller action

    Hello, i have the following action on an annotated controller:

    Code:
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ModelAndView test(
    		@RequestParam(required=false) Group group) {	
    	//return whatever
    }
    How can i call this page in order to have the group variable correctly instantiated, given that my Group class has properties like id and name with default get/set.

    I tried:
    /test?group.id=2&group.name=springrocks
    /test?group_id=2&group_name=springrocks

    Nothing worked.

    Thank you all!

  • #2
    Try this

    Code:
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ModelAndView test(
    		@ModelAttribute Group group) {	
    	//return whatever
    }
    And then access this url:

    test?id=2&name=springrocks

    Comment


    • #3
      Woot, it worked perfectly, thanks mattupstate.

      But let's suppose I have two entities at my parameter list, something like:

      Code:
      @RequestMapping(value = "/test", method = RequestMethod.GET)
      public ModelAndView test(
      		@ModelAttribute Group from,
      		@ModelAttribute Group to
      ) {	
      	//return whatever
      }
      When I access this:
      test?id=2&name=springrocks
      Both, from and to variables, get the same id and name.

      I tried something like: test?from.id=2&to.id=3
      But it failed too.

      Comment


      • #4
        Take this all with a grain of salt as I'm no Spring expert. I only started doing Java web programming last week and Spring only came into the picture last Thursday.

        At any rate, I'm sure there are a plethora of ways you could approach that, but my first reaction is that using the same object type for two semantically different items, "to" and "from", doesn't make sense to me. To keep it dead simple, when handling a form I like to create one object that holds all the form data. Maybe something named: GroupForm. That object would then have properties such as: fromId, fromName, toId, toName which each correlate to a form field in the HTTP request. The GroupForm would then be my ModelAttribute parameter. I'd then use some sort of utility or converter to convert the GroupForm to two Group objects.

        Good luck.

        Comment


        • #5
          Once again, thank you mattupstate.

          It was not exactly what I was looking for, but it's indeed a nice solution.
          Last one (I promise), let's see if you can help me on this one.

          Code:
          @RequestMapping(value = "/test", method = RequestMethod.GET)
          public ModelAndView test(
          		@ModelAttribute List<Group> listOfGroups
          ) {	
          	//return whatever
          }
          And now?

          Thank you very much, your answers were really helpful.

          Comment


          • #6
            That is a bit trickier for me and there are probably better ways. But here goes...

            I was able to deal with a list, lets say its of type List<SomeObject>, in a form by creating whats called a PropertyEditor that creates a SomeObject object from a string representation. You can read more about PropertyEditors in Spring 3 here:

            http://static.springsource.org/sprin...r-registration

            So, my form object, lets call it MyForm again, has a property on it named "objects". Its of type List<SomeObject>. And lets say the query string looks like this:

            ?objects=id:someID-name:someName&objects=id:anotherID-name:anotherName

            So long as the property editor is configured properly it will convert a string to a SomeObject object. Naturally, the property editor has to do some parsing of the string so you'd split the string on the hyphen, then split the value pairs on the colon.

            Again, there's probably a better way of doing this but this is how I achieved handling a form with a list of one type of object.

            Cheers.

            Comment


            • #7
              Hello mattupstate,

              Here is a nice solution given by axtavt from StackOverflow.
              http://stackoverflow.com/questions/3...mvc-controller

              Comment

              Working...
              X