我正在编写一个使用 Google 的 App Engine 的测试应用程序。我想使用默认的 UserService 提供登录功能。要登录,我从客户端向服务器发出请求:
public class ShoppingList implements EntryPoint {
...
private ShoppingListServiceAsync service = (ShoppingListServiceAsync) GWT.create(ShoppingListService.class);
public void onModuleLoad() {
login();
}
private void login() {
AsyncCallback callback = new AsyncCallback() {
@Override
public void onFailure(Throwable caught) {
Window.alert("Login failed.");
}
@Override
public void onSuccess(Object result) {
Window.open((String) result, "_self", "");
}
};
// Get login URL; after login, return to current page.
service.getLoginURL(GWT.getHostPageBaseURL(), callback);
}
}
服务器生成登录 URL,返回给客户端:
public class ShoppingListServiceImpl extends RemoteServiceServlet implements ShoppingListService {
...
@Override
public String getLoginURL(String returnURL) {
UserService userService = UserServiceFactory.getUserService();
Boolean loggedIn = userService.isUserLoggedIn();
String url = userService.createLoginURL("http://google.com");
User user = userService.getCurrentUser();
return returnURL;
}
}
代码编译并运行。现在,我的问题是 userService 对象以某种方式未正确加载。当我在其上调用任何方法(例如 isUserLoggedIn())时,会引发空指针异常。我错过了什么?