JSF2会话问题

时间:2014-05-30 06:15:40

标签: java facebook spring jsf

这里我在LoginBean.java中检查一个Sessionscoped bean我有这个代码

来自MyPage.xhtml

的来电
public String Method1(){
    HttpSession  secondSession =  (HttpSession) FacesContext.
                     getCurrentInstance().
                     getExternalContext().
                     getSession(false);
                    System.out.println(secondSession.getId());
}

结果来自9B4CB2748A440FEF1B2B4A7AEF401BFF

在同一个bean中,我有另一个从另一个JSF页面调用的方法 来自MyAnotherPage.xhtml

的来电
 public String Method2(){
    HttpSession  secondSession =  (HttpSession) FacesContext.
                     getCurrentInstance().
                     getExternalContext().
                     getSession(false);
                    System.out.println(secondSession.getId());
}

结果来了3F9F2F1CF9C9A0A56B44AB5DB04ABF12

看看它创建了两个不同的会话ID它意味着两个不同的会话。现在的问题是,如果在第一个方法中我在会话中添加任何属性我无法在第二个方法中获取该属性。出了什么问题为什么不同的会话对于不同的要求?

注意: - 用户从Facebook成功登录后的第二个方法调用,因此添加 Twitter APPS的是CallbackOurl。

http://127.0.0.1:8080/mywebapp/login/MyAnotherPage.xhtml 

我从这个xhtml页面调用这样的方法

<f:event listener="#{TwitterManagedBean.redirectTwitterLogin2}" type="preRenderView" />

我如何设置属性

HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
        HttpServletResponse response = (HttpServletResponse) FacesContext
                .getCurrentInstance().getExternalContext().getResponse();
        try {
            Twitter twitter = new TwitterFactory().getInstance();
            RequestToken requestToken;
            request.getSession().setAttribute("twitter", twitter);
            twitter.setOAuthConsumer("***********","***************"
                    );
            requestToken = twitter
                    .getOAuthRequestToken("http://127.0.0.1:8080/3c/login/TwitterLogin.xhtml");
            String authURL = requestToken.getAuthenticationURL();
            request.getSession().setAttribute("requestToken", requestToken);
            response.sendRedirect(authURL);
        } catch (TwitterException twitterException) {
            twitterException.printStackTrace();
        }
    }

像这样返回

 public void fetchUserInfo( )
                throws TwitterException {
            HttpServletRequest request = (HttpServletRequest) FacesContext
                    .getCurrentInstance().getExternalContext().getRequest();
            Map<String, Object> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
            Twitter value = (Twitter) requestMap.get("twitter");
            Twitter twitter = (Twitter) request.getSession()
                    .getAttribute("twitter");//Here Null
            RequestToken requestToken = (RequestToken) request.getSession()
                    .getAttribute("requestToken");//Here Null

            accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
            request.getSession().removeAttribute("requestToken");
            userId = accessToken.getUserId();
            User user = twitter.showUser(userId);
            avatarUrl = user.getProfileImageURL().toString();
            screenName = user.getScreenName();
        }

1 个答案:

答案 0 :(得分:0)

你能试试吗,这可能会解决你的问题。这对我有用。

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(true);