【问题标题】:Target Unreachable, identifier "Bean Name" resolved to null [duplicate]目标无法到达,标识符“Bean Name”解析为 null [重复]
【发布时间】:2013-07-20 05:31:48
【问题描述】:

我遇到了错误。我是 JSF 的新手。

Jul 21, 2013 6:24:04 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /index.xhtml @18,65 value="#{userbean.userName}": Target Unreachable, identifier 'userbean' resolved to null
javax.el.PropertyNotFoundException: /index.xhtml @18,65 value="#{userbean.userName}": Target Unreachable, identifier 'userbean' resolved to null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.panel.Panel.processValidators(Panel.java:284)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

这是我的代码:

package com.jsf.dev.bean;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;

@ManagedBean(name = "userbean")
public class UserBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private String userName;
    private String userPassword;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String login(){
        return "login";
    }
}

XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head>
<h:body>
    <center>
        <p:panel header="Login Form" style="width:350;">
            <h:form>
                <h:panelGrid columns="2" cellpadding="2">
                    <h:outputLabel for="#{userbean.userName}" value="UserName" />
                    <h:inputText value="#{userbean.userName}" label="UserName"></h:inputText>
                    <h:outputLabel for="#{userbean.userPassword}" value="Password" />
                    <h:inputSecret value="#{userbean.userPassword}"></h:inputSecret>
                    <h:commandButton type="submit" value="Login"
                        action="#{userbean.login}"></h:commandButton>
                </h:panelGrid>
            </h:form>
        </p:panel>
        <div>
            <h:messages></h:messages>
        </div>
    </center>
</h:body>
</html>

【问题讨论】:

  • 你的 UserBean 的作用域是什么?
  • 请发布“faces-config.xml”和“web.xml”

标签: jsf primefaces


【解决方案1】:

h:outputLabel 中,for 属性必须在 JSF 生命周期的较早阶段计算为 String,而不是表达式 "#{userbean.userName}" 可以计算。因此,您需要给它一个这样的字符串值:

<h:outputLabel for="userName" value="UserName" />
<h:inputText id="userName" value="#{userbean.userName}" label="UserName"/>

请参阅this link 了解 JSF 生命周期:

恢复视图阶段 在这个阶段,JavaServer Faces 实现构建页面的视图...... 如果对页面的请求是初始请求,JavaServer Faces 实现在此阶段会创建一个空视图,并且 生命周期进入渲染响应阶段,在此期间 空视图由标签引用的组件填充 页面。

因此,在(空)视图已经构建时,不在渲染响应阶段之前评估 bean 值。

【讨论】:

  • 已经按照你说的修改了代码。但点击按钮后仍然出现同样的错误
  • 我猜你已经将第二个&lt;h:outputLabel&gt;相应地更改为&lt;h:inputSecret&gt;
  • 大家好,非常感谢已经解决了我只是用 maven 目标 tomcat:run-war 而不是 tomcat:run 运行它的问题
  • 好叫 Shankar,这对我有用,这也是我的运行命令,总共“干净安装 tomcat7:run-war”如果有人能解释为什么这可以解决这个特殊问题,那将会很有帮助跨度>
猜你喜欢
  • 2014-02-10
  • 2019-05-10
  • 2022-12-12
  • 2015-05-18
  • 1970-01-01
  • 2016-03-09
  • 2012-02-22
  • 2014-06-12
  • 2020-09-28
相关资源
最近更新 更多