【问题标题】:jsf target unreachable, identifier [bean] resolved to null [duplicate]jsf 目标无法访问,标识符 [bean] 解析为 null [重复]
【发布时间】:2019-05-10 21:56:06
【问题描述】:

通过如下链接挖掘后,我无法解决问题:Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

我收到以下错误:javax.servlet.ServletException: /Project9.xhtml @13,55 value="#{ProjectBean.income}": Target Unreachable, identifier [ProjectBean] 解析为 null

我的表格:

<!DOCTYPE html>
 <html xmlns="http://wwww3.org/1999/xhtml"
  xmlns:a="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  >
<h:head>
<title>Project9</title>
</h:head>

<h:body>
<h:form>
    Income: <h:inputText value="#{ProjectBean.income}"/>
    Number of people: <h:inputText value="#{ProjectBean.numPeople}"/>
        <h:commandButton value= "Submit" action= "Project9response"/>
</h:form>

</h:body>
</html>

我不确定正确的约定是 wwww3.org 还是 www.3.org,但我都尝试过。

我的回复页面:

<!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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  >
<h:head>
<title><ui:insert name="title">Project 9 response</ui:insert></title>
</h:head>


<h:body>
Am I above the poverty level: #{ProjectBean.abovePovertyLevel()}

</h:body>

</html>

我的豆子:

@ManagedBean
@SessionScoped
public class ProjectBean implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private double income;
private double numPeople;

//constructor is no arg
public ProjectBean() {

}

public double getIncome() {
    return income;
}
public void setIncome(double income) {
    this.income = income;
}
public double getNumPeople() {
    return numPeople;
}
public void setNumPeople(double numPeople) {
    this.numPeople = numPeople;
}

public boolean abovePovertyLevel() {
    if ( income < (16460.00 + 4320.00) * (numPeople - 2)){
        return false;
    }
    else {
    return true;
}
}

}

我的面孔-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
version="2.3">

</faces-config>

我的 faces-config.xml 和 javax-faces.2.2.8.jar 文件在 WEB-INF 的 lib 文件夹中 我的 bean 在 Java Resources 的 src 文件夹中

我有另一个名为 helloworld 的项目,其中有几个可以正常工作的小 JSF 项目,我尝试将 project9 复制到 helloworld 并运行它,但我只在那个项目上遇到同样的错误

我已尝试按照人们的建议清理我的项目

这是一个学生项目,也是我对 JSF 和 Tomcat 的第一次介绍。我正在使用 Mac 和 Eclipse 光子。

正如我在代码中的评论中所说,如果我尝试绕过响应 xhtml 文件并直接进入我的 html 表单中的 javabean 方法,我会在 ProjectBean.abovePovertyLevel() 下得到一个下划线,并且错误“操作值与导航案例结果不匹配”

我不确定上面链接中关于 CDI 等的一些答案,目前这一切都在我的脑海中。

【问题讨论】:

    标签: jsf el propertynotfoundexception


    【解决方案1】:

    将所有#{ProjectBean. 替换为#{projectBean.(使第一个字符小写)。

    这是因为您需要通过名称而不是类名来引用托管 bean。默认情况下,jsf 会根据https://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html 的文档为您生成此名称:

    如果 name 属性的值未指定或为空字符串,则 managed-bean-name 派生自完全限定类名的非限定类名部分并将第一个字符转换为小写。例如,如果 ManagedBean 注解在具有完全限定类名称 com.example.Bean 的类上,并且注解上没有 name 属性,则将 managed-bean-name 视为 bean。

    【讨论】:

      【解决方案2】:

      没关系,我刚刚解决了这个问题 为我的注释添加了一个名称,如下所示:@ManagedBean(name="ProjectBean")

      我也意识到如果我不先声明贫困线,我的方法是错误的

      int povertyLevel = (16460 + 4230 * (numPeople-2));
          if(income > povertyLevel)
              return true;
          else{
          return false;
          }
      

      这似乎有效

      【讨论】:

        猜你喜欢
        • 2015-09-14
        • 2013-10-16
        • 2013-07-20
        • 2015-05-18
        • 2012-04-06
        • 1970-01-01
        • 2014-08-24
        • 2012-10-17
        • 2018-05-09
        相关资源
        最近更新 更多