2010-10-19 43 views
3

爲了發送適當的響應,我需要檢測控制器操作是否已經被傳統的HTTP GET請求,AJAX請求或g:include標籤lib請求。如何檢測控制器已被g調用:include tag lib?

例如,考慮下面的代碼片段代碼:

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] 
    } 
} 

...我怎樣才能檢測到動作已通過A G稱爲:包括標記庫?

謝謝。

回答

5

你可以這樣測試:

import org.springframework.web.util.WebUtils 

if (request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)) { 
    // request was included 
} 
+0

感謝。我會嘗試並更新我的答案 – fabien7474 2010-10-28 22:20:22

+0

它的作用就像一個魅力! – fabien7474 2010-10-31 19:11:47