tags:

views:

31

answers:

1

In order to send appropriate response, I need to detect whether the controller action has been requested by a classic HTTP GET request, an AJAX request or a g:include tag lib.

For instance, considering the following snippet code:

class CommunityController {
  def show = {
    def users = getUsers()
    if (/* WHAT IS THE CODE HERE??? */)  //g:include request => render 'show' template only
      render template:'show', model=[users]
    else if (request.xhr)  //Ajax => we send JSON content
      render users as JSON
    else //Classic request => we render 'show' GSP page
      [users]
  }
}

...how can I detect that the action has been called via a g:include tag lib?

Thank you.

+1  A: 

You can test it like this:

import org.springframework.web.util.WebUtils

if (request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)) {
    // request was included
}
ataylor
Thanks. I'll try it and update my answer
fabien7474
It works like a charm!
fabien7474