我在这里检查在LoginBean.java一个会话范围的bean,我有这个代码
MyPage.xhtml
呼叫
public String Method1(){
HttpSession secondSession = (HttpSession) FacesContext.
getCurrentInstance().
getExternalContext().
getSession(false);
System.out.println(secondSession.getId());
}
结果即将到来
在同一个bean中,我有另一个从另一个JSF页面调用的方法MyAnotherPage.xhtml
呼叫
public String Method2(){
HttpSession secondSession = (HttpSession) FacesContext.
getCurrentInstance().
getExternalContext().
getSession(false);
System.out.println(secondSession.getId());
}
结果即将到来
看,它创建了两个不同的会话id,这意味着两个不同的会话。现在的问题是,如果在第一个方法中我在会话中添加任何属性,我无法在第二个方法中获得该属性。出了什么问题为什么不同的会话不同的请求?
注意:-用户成功从Facebook登录后的第二个方法调用,所以它是CallbackOurl添加Twitter APPS。
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();
}
你可以试试这个,也许这会解决你的问题。这对我很有用。
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(true);