Iva*_*tyk 1 ejb cdi twitter4j jsf-2 conversation-scope
我正在尝试实现Twitter登录过程.在登录过程中,用户需要被重定向到Twitter网站以输入他/她的凭证,然后他/她将被重定向到我的网站URL.在第一次重定向之前,应该在请求之间存储和保留RequestToken对象(Twitter4J库)的实例.
为此,我决定使用ConverstaionScoped bean,但遗憾的是,请求之间不保留引用的值.
这是我的JSF页面:
twitter.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<f:event listener="#{twitterLoginPageController.login()}" type="preRenderView" />
</html>
Run Code Online (Sandbox Code Playgroud)
twitterRedirect.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<f:event listener="#{twitterLoginPageController.onRedirect(param.oauth_token, param.oauth_verifier)}" type="preRenderView" />
</html>
Run Code Online (Sandbox Code Playgroud)
我的Twitter登录控制器:
@Named
@ConversationScoped
public class TwitterLoginPageController implements Serializable {
@Inject
private Conversation conversation;
// These objects should be retained between requests
Twitter twitter;
RequestToken requestToken;
public void login() {
try {
twitter = new TwitterFactory().getInstance();
requestToken = twitter.getOAuthRequestToken("...");
conversation.begin();
conversation.setTimeout(30000);
// Navigate to Twitter here
} catch (TwitterException ex) {
...
}
}
public void onRedirect(String oauthToken, String oauthVerifier) {
try {
// twitter reference is null here
twitter.getOAuthAccessToken(requestToken, oauthVerifier);
...
} catch (TwitterException e) {
...
} finally {
conversation.end();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我正在密切关注使用@ConverstaionScope的例子,但是我没有得到预期的结果.如何在请求之间保留对象?
归档时间: |
|
查看次数: |
320 次 |
最近记录: |