我为我开发的简单登录应用程序收到以下错误。 我已经在我的托管bean中提供了Session Scopen,并且可以使用我的setter和getter。那么为什么会出错?
XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<h2>Welcome </h2>
<h:panelGrid columns="2">
<h:outputText value="Username" id="userName"></h:outputText>
<h:inputText id="user" value="#{login.credential.username}" /><h:outputText value="Password" id="userPassword"></h:outputText>
<h:inputText id="password" value="#{login.credential.password}" />
</h:panelGrid>
<h:commandButton action="success" value="Login" />
</h:form>
</h:body>
</html>
Login.java
package com.practice;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="login")
@SessionScoped
public class Login
{
private Credential credential;
public Login(){
System.out.println("Login called");
}
public Credential getCredential() {
return credential;
}
public void setCredential(Credential credential) {
this.credential = credential;
}
public String loginProcess(){
System.out.println("Method called");
return "success";
}
}
Credential.java
package com.practice;
import java.io.Serializable;
public class Credential implements Serializable {
private String username;
private String password;
public Credential(){
System.out.println("Credential called");
}
public String getUsername() {
System.out.println("Get username called");
return username;
}
public void setUsername(String username) {
System.out.println("Set username called");
this.username = username;
}
public String getPassword() {
System.out.println("Get password called");
return password;
}
public void setPassword(String password) {
System.out.println("Set password called");
this.password = password;
}
}
例外:
javax.servlet.ServletException: /index.xhtml @16,64 value="#{login.credential.username}": JBWEB006017: Target Unreachable, ''credential'' returned null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
JBWEB000071: root cause
javax.el.PropertyNotFoundException: /index.xhtml @16,64 value="#{login.credential.username}": JBWEB006017: Target Unreachable, ''credential'' returned null
com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
javax.faces.component.UIInput.validate(UIInput.java:960)
javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
javax.faces.component.UIInput.processValidators(UIInput.java:698)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
javax.faces.component.UIForm.processValidators(UIForm.java:253)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1164)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
请帮忙。
答案 0 :(得分:0)
在您发布的代码中,credential
属性从未初始化。我们你从外面的某个地方打电话给setCredential()
吗?
尝试扩充构造函数以初始化默认值(因为根据定义,credential
为null
public Login(){
System.out.println("Login called");
credential = new Credential();
}
如果你在其他地方打电话给setCredential()
,你可能会认为你打电话太晚了 - 可能是在对房产进行评估显示之后。