JSF和ManagedBean:NullPointerException

时间:2013-05-28 12:57:45

标签: java jsf nullpointerexception managed-bean

我正在使用托管bean设计JSF应用程序。 目前,我一直在尝试创建一个简单的登录页面(用户名和密码暂时是硬编码的):

<h:form class="form-signin">
    <h2 class="form-signin-heading">Please sign in</h2><hr />
    <input name="username" type="text" class="input-block-level" placeholder="Username" />
    <input name="password" type="password" class="input-block-level" placeholder="Password" />
    <h:commandButton action="#{userController.login}" class="btn btn-block btn-primary" type="submit" value="Sign in" />
</h:form>

这是控制器(UserController.java):

@ManagedBean(name="userController")
@ApplicationScoped
public class UserController {

    @EJB
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserController() {
    }

    public void login() throws IOException {
        Boolean login = userService.login("admin", "p4ssw0rd");
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        if (login == true) { 
            externalContext.redirect("dashboard.xhtml");
        } else {
            externalContext.redirect("login.xhtml");
        }
    }
}

这是UserService.java文件:

@Stateless
public class UserService {

    @PersistenceContext
    private EntityManager em;

    public static String md5(String input) {
        // Removed for clarity...
    }

    public Boolean login(String username, String password) {
        //String hash = md5(password);
        return Boolean.TRUE; // As you can see, nothing can fail for the moment
    }

}

当我提交登录表单时,会出现NullPointerException:

javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at com.myname.myproject.managedbean.UserController.login(UserController.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

在UserController.java中,如果我替换此行:

Boolean login = userService.login("admin", "p4ssw0rd");

通过这个:

Boolean login = true; // Or false (I've tested twice)

一切正常,所以看起来Java无法找到UserService ......

感谢您的帮助,我完全迷失了。

3 个答案:

答案 0 :(得分:0)

您是否检查过托管bean中的getter和setter,名为UserController?

如果没有,即使代码编译正确,依赖注入也无法正确保存。

答案 1 :(得分:0)

我认为问题是EJB。如果它是无接口bean,则应注释@LocalBean,否则应实现接口。尝试这样的事情:

@Stateless
public class UserService implements UserServiceLocal {

    @Override
    public boolean login () {
        //dummy implementation
        return true;
    }
}

其中UserServiceLocal是:

@Local
public interface UserServiceLocal {

    public boolean login();
}

用法:

@ManagedBean(name="userController")
@ApplicationScoped
public class UserController {

    @EJB
    private UserServiceLocal userService;

    public void login() {
       userService.login();
    }
}

答案 2 :(得分:0)

关于为什么要实施界面的问题,请参阅EJB's - when to use Remote and/or local interfaces?。 EJB(企业java bean)可以有一个接口(可以用@Remote注释,这意味着实现它的bean在分布式环境中运行,或者用@Local注释意味着bean在同一个JVM中运行。相反,无接口bean是一个不实现任何接口的bean。因此,您应该指示JVM将其视为bean而不是POJO(普通的旧Java对象);这可以通过在类上添加@LocalBean批注来实现。它应该是你的bean / EJB。