1

I have a test JSF Page:

<!DOCTYPE html>
<html xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http:/java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<h:body>
<h:form>
    <h:commandButton id="testButton" value="test" action="#{sessionHandler.login}"></h:commandButton>
</h:form>
</h:body>
</html>

The commandButton calls a backing bean called SessionHandler:

import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class SessionHandler {
    @Inject
    private SessionEJB session = new SessionEJB();
    public SessionHandler(){}
    public String login(){
        return "video.xhtml";
    }
}

The problem is when I click the button, its not finding the backing bean method. I'm getting this error:

javax.servlet.ServletException: javax.el.PropertyNotFoundException: /test.xhtml @10,94 action="#{sessionHandler.login}": Target Unreachable, identifier 'sessionHandler' resolved to null

root cause

javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: /test.xhtml @10,94 action="#{sessionHandler.login}": Target Unreachable, identifier 'sessionHandler' resolved to null

root cause

javax.el.PropertyNotFoundException: /test.xhtml @10,94 action="#{sessionHandler.login}": Target Unreachable, identifier 'sessionHandler' resolved to null

Why is it not finding my backing bean?

4

3 回答 3

2

似乎您正在混合 JSF bean 和 CDI bean,这是您无法做到的。只需将导入替换为javax.faces.bean.SessionScopedjavax.enterprise.context.SessionScoped您就可以使用 CDI。

SessionScoped bean 也应该具有钝化能力,因此实现Serializable.

于 2014-09-15T19:03:57.730 回答
2

我发现了这个问题,我发布的原始代码看不到这个问题,所以,我为此道歉。尽管错误指出 sessionHandler 被解析为 null,但实际上无法找到我引用的 EJB。这是因为我将 EJB Jar 导入构建路径,而不是将其放入 WEB-INF/lib 文件夹。虽然,我相信合适的方法是创建一个企业应用程序项目并同时引用 EJB Jar 项目和 Web 项目。所以,我的问题与我的代码无关,而与我引用项目的方式有关。我希望这可以帮助任何面临类似问题的人。

于 2014-09-17T17:12:23.520 回答
1

在 Java EE 中,有两种方法可以为 JSF 声明 bean。使用旧的托管 bean 设施:

@javax.faces.bean.ManagedBean
@javax.faces.bean.SessionScoped
public class SessionBean {}

或使用 CDI(现在推荐的方式)

@javax.inject.Named
@javax.enterprise.context.SessionScoped
public class SessionBean {}

要启用 CDI,您还需要类路径中的 beans.xml

于 2014-09-15T19:42:02.177 回答