JSF - Random Number using Beans (JAVA)

Posted by Alex Encore Tr on Stack Overflow See other posts from Stack Overflow or by Alex Encore Tr
Published on 2012-04-14T17:25:46Z Indexed on 2012/04/14 17:28 UTC
Read the original article Hit count: 183

Filed under:
|
|
|
|

I am trying to create a jsf application which, upon page refresh increments the hit counter and generates two random numbers. What should be displayed on the window may look something like this:

On your On your roll x you have thrown x and x

For this program I decided to create two Beans, one to hold the page refresh counter and one to generate a random number. Those look like this for the moment:

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;
    }
}

I have then created an index.jsp to display the above message.

<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>

However, when I run the application, I get the following message:

org.apache.jasper.el.JspPropertyNotFoundException: /index.jsp(14,20) '#{RandomNumberBean.rand}' Property 'rand' not found on type diceroll.RandomNumberBean

Caused by: org.apache.jasper.el.JspPropertyNotFoundException - /index.jsp(14,20) '#{RandomNumberBean.rand}' Property 'rand' not found on type diceroll.RandomNumberBean

I suppose there's a mistake with my faces-config.xml file, so I will post this here as well, see if somebody can provide some help:

faces-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>

© Stack Overflow or respective owner

Related posts about java

Related posts about jsf