GWT RPC 范围问题
我在保存 RPC 结果时遇到问题。基本上调用是成功的,但是当我尝试将其保存在变量中时,它似乎超出了范围。
private String userName = "default";
authService.retrieveUserData(new AsyncCallback<UserData>() {
@Override
public void onSuccess(UserData result) {
userName = result.getUserName();
shell.setError("The userName is " + userName + "!");
}
@Override
public void onFailure(Throwable caught) {
//I've checked and it is not failing.
}
});
shell.getLoginWidget().setUserName(userName);
shell 是一个添加到我的 RootLayoutPanel 中的 Composite。它包含一个 DivElement,其 InnerText 由 shell.setError(String) 设置。 shell 还有一个带有 SpanElement 的 LoginWidget,其 InnerText 通过 shell.getLoginWidget().setUserName(String) 设置。 userName 显然是一个初始化为“default”的字符串。
问题是 shell.setError("The userName is " + userName + "!"); (在 AsyncCallback 内部)将 InnerText 设置为当前登录的用户。但是 shell.getLoginWidget().setUserName(userName); (AsyncCallback 之外)将 InnerText 设置为“默认”。
我尝试了很多不同的事情,它们似乎都有相同的范围问题。真正让我困惑的是这个示例显示的内容几乎完全相同保存 RPC 结果的技术。
I'm having a problem saving the result of a RPC. Basically the call is successful but when I try to save it in a variable it seems to go out of scope.
private String userName = "default";
authService.retrieveUserData(new AsyncCallback<UserData>() {
@Override
public void onSuccess(UserData result) {
userName = result.getUserName();
shell.setError("The userName is " + userName + "!");
}
@Override
public void onFailure(Throwable caught) {
//I've checked and it is not failing.
}
});
shell.getLoginWidget().setUserName(userName);
shell is a Composite that is added to my RootLayoutPanel. It contains a DivElement of which the InnerText is set by shell.setError(String). shell also has a LoginWidget with a SpanElement of which the InnerText is set by shell.getLoginWidget().setUserName(String). userName is obviously a String that is initialized to "default".
The problem is that the shell.setError("The userName is " + userName + "!"); (inside the AsyncCallback) sets the InnerText to whoever is currently logged in. BUT shell.getLoginWidget().setUserName(userName); (outside the AsyncCallback) sets the InnerText to "default".
I have tried so many different things and they all seem to have the same scope problem. What really confuses me is that this example shows almost exactly the same technique for saving the result of the RPC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GWT RPC 方法是异步 - 它们的返回值无法立即可用。您发布的代码的控制流程如下:
userName
设置为“default”。authService
询问UserData
。这会为“UserData.xml”触发异步XMLHttpRequest
。userName
设置为userName
的当前值,即“默认”。userName
变量将设置为返回值。正确的做法是在
AsyncCallback
的onSuccess
方法中设置用户名:GWT RPC methods are asynchronous - their return values are not available immediately. The flow of control for the code you posted is as follows:
userName
to "default".authService
for theUserData
. This fires off an asynchronousXMLHttpRequest
for the `UserData.userName
in the login widget to the current value ofuserName
, which is "default".userName
variable will be set to the returned value.The correct thing to do is to set the user name in the
onSuccess
method of theAsyncCallback
: