0

How do you map a Url with two ids:

/orders/$id1/orderlines/$id2

The id2 is optional

/orders/$id1/orderlines GET -> all orderlines for order id1 /orders/$id1/orderlines/$id2 GET -> show orderline id2 within order id1

The methods are to be mapped to OrderLineController

It is pretty easy with Spring MVC @RequestMapping and @PathVariable.

Grails 3 does not allow @RequestMapping (there are are tricks to make it work - but I don't want to go that route - needlessly complex).

Appreciate the help. I did quite a bit of googling.

Nabil
  • 71
  • 1
  • 4

2 Answers2

1

You can use UrlMappings:

UrlMappings.groovy

 class UrlMappings {
        static mappings = {
            "/orders/$id1/orderlines"(controller: 'orderLine', action: 'someAction1')
            "/orders/$id1/orderlines/$id2"(controller: 'orderLine', action: 'someAction2')
        }
    }

OrderLineController.groovy

def someAction1(Long id1){
  //...
}
def someAction2(Long id1, Long id2){
  //...
}
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
0

Using nested URLMappings:

UrlMappings.groovy

class UrlMappings {
  static mappings = {
    "/orders"(resources:"order") {
      "/orderLines"(resources:"orderLine")
    }
  }
}

OrderController.groovy

def show() {
  params.id // Order.id
}

OrderLineController.groovy

def show() {
  params.orderId // Order.id
  params.id      // OrderLine.id
}

As stated by @dmahapatro, check out the documentation on Nested Resources.

thewidgetsmith
  • 194
  • 1
  • 8
  • Thanks very much for this elegant solution. The only change I needed was to put both methods in OrderLineController. I don't need to do anything to the OrderController, it's working fine. So I coded the index() method to handle the one param (order.id), and the show() method handles the two params. – Nabil Feb 03 '17 at 01:10