2013-04-13 36 views
0

我是GWTP的新手。我正在嘗試使用GWTP + Command Pattern + Spring構建應用程序。如何從GWTP CommandHandler訪問Http Session

目前我的應用程序有一個使用Spring控制器的登錄頁面,它將用戶重定向到GWT頁面。在Spring控制器中,我們可以訪問會話,因爲我們可以訪問HttpRequest對象。在GWTP命令處理程序的情況下,我找不到任何方法來訪問HttpSession。 以下是我的命令處理程序代碼 -

public class GetItemsCommandHandler extends AbstractActionHandler<GetItemsCommand, GetItemsResult>{ 

    @Autowired 
    private ItemService itemService; 

    public GetItemsCommandHandler() { 
     super(GetItemsCommand.class); 
    } 

    @Override 
    public GetItemsResult execute(GetItemsCommand action, ExecutionContext context) throws ActionException { 
     // How to get User info(userid) from session?? 
     GetItemsResult getItemsResult = new GetItemsResult(); 
     getItemsResult.setItems(itemService.getItemsForUser(userId)); 
     return getItemsResult; 
    } 

} 

有什麼辦法,我可以從GWTP命令處理程序訪問HttpSession的?

+0

使用guice ??? –

回答

0

Thanks @Baadshah但我在服務器端使用Spring。順便說一句,我得到的解決方案,我張貼如果有人面臨類似的問題。

首先,我們需要在web.xml中

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

RequestContextListener添加下面的條目是公開請求當前線程的監聽器。

然後我們可以使用@Autowired註解。

@Autowired 
private HttpServletRequest httpServletRequest; 

和處理方法,我們可以從這個HttpServletRequest的會話中使用的getSession方法。