问题
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
- The annotation
@ManagedBean(make sure to useimport javax.faces.bean.ManagedBean;)! - A public no-arg constructor
- Getters and setters for
usernameandpassword
回答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