2011-08-30 52 views
3

在玩!控制器,我可以創建一個interceptor method,它將在每個請求到達適當的操作之前處理它。有沒有一種方法可以在播放框架中的攔截器和操作之間共享數據?

public class Admin extends Application { 

    @Before 
    static void checkAuthentification() { 
     if(session.get("user") == null) login(); 
     // otherwise, 
     User loggedOnUser = User.find("byUsername", session.get("user")); 
    } 

    public static void index() { 
     // any way to access loggedOnUser ? 
     List<User> users = User.findAll(); 
     render(users); 
    } 
    … 
} 

有沒有辦法在攔截器中設置一個值並在action中進行訪問?有點像servlet中的request.setAttribute()

回答

2

攔截器和操作共享相同的請求上下文(請求,響應,會話等)。如上所述,您可以選擇使用renderArgs,但請記住,這些值將在您的視圖中可用,這可能不是您想要的。如果你想保持攔截器和動作之間的狀態,只需使用request.args散列即可。

4

您可以使用Controller中的renderArgs參數(請參閱here),或者您可以將值存儲在緩存中(我們可以假定該值是在毫秒前添加的,您的值在同一請求中可用)。

+0

嘿,那是一個脆弱的環節。 – itsadok

+0

@itsadok是,短期參考:P –

相關問題