gpt4 book ai didi

java - JSF:目标无法访问, 'value' 在 JSF 中返回 null?

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:55 24 4
gpt4 key购买 nike

对于我开发的简单登录应用程序,我收到以下错误。我已经在我的托管 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;
}
}

异常(exception):

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)

请帮忙。

最佳答案

在您发布的代码中,credential 属性从未初始化。您是否从外部某个地方调用 setCredential()

尝试扩充构造函数以初始化默认值(因为根据定义,credentialnull:

public Login(){
System.out.println("Login called");
credential = new Credential();
}

如果您确实在其他地方调用 setCredential(),您可能会认为调用它为时已晚 – 也许是在评估属性以进行显示之后。

关于java - JSF:目标无法访问, 'value' 在 JSF 中返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com