Announcement Announcement Module
Collapse
No announcement yet.
Using Spring 3 path variables Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Spring 3 path variables

    I'm playing with Grails and trying to port an existing Spring 3 app.

    I'm having trouble with @RequestMapping and @PathVariables of the following form:

    Code:
    @Controller
    @RequestMapping("/spring")
    class SpringController {
    
        @RequestMapping(value = "test/{var1}/{var2}/done", method = RequestMethod.GET)
        ResponseEntity<String> whoa(@PathVariable String var1, @PathVariable String var2)
        {
            println "Called "+var1+" "+var2
            HttpHeaders headers = new HttpHeaders();
            headers.contentType = MediaType.TEXT_PLAIN;
            return new ResponseEntity<String>("Wootlez^max", headers, HttpStatus.OK)
        }
    }
    Request:
    Code:
    /spring/test/x/y/done
    Output:
    Code:
    Called (*) (*)
    It would also seem that the only way to get Grails to dispatch to Spring's url handler is to map my RESTful url to RESTful url.dispatch.

    So -- URLMappings.groovy:
    Code:
    "/spring/test/$var1/$var2/done"(uri:"""/spring/test/$var1/$var2/done.dispatch""")
    Anyone know how to get these path variables bound to 'x' and 'y'?
    Bonus. Anyone know how to avoid having to specify URLMappings.groovy lines in addition to my Spring MVC annotations?
    Last edited by jhazen; May 9th, 2011, 01:37 PM. Reason: cleanup oops

  • #2
    Simple, when you have a url pathmapping like /somepath/$variable

    $variable is going to be bound to params.variable

    Comment


    • #3
      Existing Spring 3 controllers don't make use of a params variable and I'd like to use the existing code without modifications if possible.

      Also, when I try and inject the params variable as if it were a grails controller I get the following error:

      Error 500: No such property: params for class: grailstest.SpringController

      Def'ing a params variable for injecting and then attempting to use it yields:

      Error 500: Cannot get property 'var1' on null object

      It would seem that if Grails is dispatching to Spring 3 it's not going to treat this controller as a Grails controller, which is fine, since I'm really just looking for Spring 3 MVC support at this point.

      Comment


      • #4
        The only thing I can think of is to add '.dispatch' to the URL in your @RequestMapping annotation.

        Comment


        • #5
          No change.

          Comment


          • #6
            Solution:

            Only changes required from default 1.3.7 grails new-app (No updates to UrlMappings, no .dispatch in request uri):

            grails install-templates

            add the following to web.xml

            Code:
            <servlet-mapping>
                    <servlet-name>grails</servlet-name>
                    <url-pattern>/</url-pattern>
            </servlet-mapping>
            url-pattern must be / not /*

            Credit to this 3 year old post: http://forum.springsource.org/showth...019#post168019
            Last edited by jhazen; May 15th, 2011, 02:33 PM.

            Comment


            • #7
              Hi, do you have a sample application demonstrating this? I can't seem to get it working with Grails 1.3.7 based on the info in this thread.

              Thanks.

              Comment

              Working...
              X