2

通过以下链接挖掘后,我无法解决问题:识别和解决 javax.el.PropertyNotFoundException: Target Unreachable

我收到以下错误: javax.servlet.ServletException: /Project9.xhtml @13,55 value="#{ProjectBean.income}": Target Unreachable, identifier [ProjectBean] resolve to 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>

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

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

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

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

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

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

4

2 回答 2

3

替换所有#{ProjectBean.by #{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。

于 2018-12-10T01:09:16.590 回答
2

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

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

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

这似乎有效

于 2018-12-10T01:08:56.487 回答