GWT中的UserService

时间:2013-12-03 13:58:26

标签: java gwt gae-userservice

我正在编写一个使用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()),抛出空指针异常。我错过了什么?

1 个答案:

答案 0 :(得分:0)

由于您没有发布UserServiceFactory代码,因此很难找出问题的根源。但我想在您的工厂中没有正确实现UserService-Instance。