JSF: /index.xhtml @12,80 value=“#{LoginBean.username}”: Target Unreachable, identifier 'LoginBean' resolved to null [duplicate]

ぃ、小莉子 提交于 2019-12-11 04:54:09

问题


I'm new to JSF and can't figure out what's going on.
I keep getting this error: /index.xhtml @12,80 value="#{LoginBean.username}": Target Unreachable, identifier 'LoginBean' resolved to null

I've isolated the problem down to this...

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 LoginApp</title>
</h:head>
    <h:body>
        <h2>JSF 2.0 Login App</h2><br/>
        <h:form>
            Username: <h:inputText id="username" value="#{LoginBean.username}">  //error here
            </h:inputText> <br/><br/>
            Password: <h:inputSecret id="password" value="#{LoginBean.password}">  //and error here
            </h:inputSecret> <br/><br/>
            <h:commandButton action="response.xhtml" value="Login" type="Submit"></h:commandButton>
        </h:form>


    </h:body>
</html>

response.xhtml

    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Login Response</title>
    </h:head>
    <h:body>
        <h:outputText id="result" escape="false" value="#{LoginBean.authenticate}"/>
    </h:body>
</html>

LoginBean.java

public class LoginBean {
private String username;
private String password;
}

回答1:


Don´t use Managedbean anymore.

Switch to CDI instead

Add this on top of your class @RequestScoped ( from javax.enterprise.context.RequestScoped ) @Named("LoginBean")

see http://docs.oracle.com/javaee/6/tutorial/doc/gjbnr.html for further informations




回答2:


You should fix your bean name in jsf EL.I think you didn't give a name to your bean.Therefore in jsf EL, your bean name must start with lower case.It is jsf default.

Change value="#{LoginBean.username}" to value="#{loginBean.username}"




回答3:


Make sure your bean has

  1. The annotation @ManagedBean (make sure to use import javax.faces.bean.ManagedBean;)!
  2. A public no-arg constructor
  3. Getters and setters for username and password



回答4:


Make sure your loginBean is annotated with @ManagedBean. If your class name is LoginBean then identifier in EL becomes loginBeanas per java conventions and not LoginBean



来源:https://stackoverflow.com/questions/19268694/jsf-index-xhtml-12-80-value-loginbean-username-target-unreachable-iden

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!