我正在尝试创建一个 jsf 应用程序,该应用程序在页面刷新时会增加命中计数器并生成两个随机数。应该在窗口上显示的内容可能如下所示:
On your roll x 你扔了 x 和 x
对于这个程序,我决定创建两个 Bean,一个用于保存页面刷新计数器,一个用于生成随机数。目前看起来像这样:
CounterBean.java
package diceroll;
public class CounterBean
{
int count=0;
public CounterBean()
{
}
public void setCount(int count)
{
this.count=count;
}
public int getCount()
{
count++;
return count;
}
}
RandomNumberBean.java
package diceroll;
import java.util.Random;
public class RandomNumberBean {
int rand=0;
Random r = new Random();
public RandomNumberBean()
{
rand = r.nextInt(6);
}
public void setNextInt(int rand)
{
this.rand=rand;
}
public int getNextInt() {
return rand;
}
}
然后我创建了一个 index.jsp 来显示上述消息。
<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Roll the Dice</title>
</head>
<body>
<h:form>
<p>
On your roll #
<h:outputText value="#{CounterBean.count} " />
you have thrown <h:outputText value="#{RandomNumberBean.rand}" />and <h:outputText value="#{RandomNumberBean.rand} " />
</p>
</h:form>
</body>
</f:view>
</html>
但是,当我运行应用程序时,我收到以下消息:
org.apache.jasper.el.JspPropertyNotFoundException: /index.jsp(14,20) '#{RandomNumberBean.rand}' 在类型 diceroll.RandomNumberBean 上找不到属性 'rand'
引起:org.apache.jasper.el.JspPropertyNotFoundException - /index.jsp(14,20) '#{RandomNumberBean.rand}' 在类型 diceroll.RandomNumberBean 上找不到属性 'rand'
我想我的 faces-config.xml 文件有错误,所以我也会在这里发布,看看是否有人可以提供一些帮助:
面孔-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>CounterBean</managed-bean-name>
<managed-bean-class>diceroll.CounterBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>RandomNumberBean</managed-bean-name>
<managed-bean-class>diceroll.RandomNumberBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>