Results 1 to 7 of 7

Thread: Using Spring 3 path variables

  1. #1
    Join Date
    Apr 2005
    Posts
    25

    Default 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 at 01:37 PM. Reason: cleanup oops

  2. #2
    Join Date
    Apr 2008
    Posts
    151

    Default

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

    $variable is going to be bound to params.variable

  3. #3
    Join Date
    Apr 2005
    Posts
    25

    Default

    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.

  4. #4
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

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

  5. #5
    Join Date
    Apr 2005
    Posts
    25

    Default

    No change.

  6. #6
    Join Date
    Apr 2005
    Posts
    25

    Default

    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 at 02:33 PM.

  7. #7
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

    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.

Posting Permissions

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