user2966197
user2966197

Reputation: 2981

Managed bean not constructed

I am trying to display a random number on a panel through PrimeFaces panel. I have the following xhtml code:

<h:form>
    <p:growl id="msgs" showDetail="true" />
    <p:panel id="basic" header="Random Number"  style="margin-bottom:20px">
        <h:panelGrid columns="2" cellpadding="10">
            <h:outputText value="#{randomnum.number}" />
        </h:panelGrid>
    </p:panel>  
</h:form>

This panel calls randomnum.number which is like this

import java.io.Serializable;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped

public class randomnum implements Serializable {

    private int number;
    public randomnum() {            
    }       

    public int getNumber() {            
        return number;  
    }  

    @PostConstruct
    public void init() {
        Random r = new Random();
        int Low = 10;
        int High = 100;
        number = r.nextInt(High-Low) + Low;
        System.out.println("Random Number :"+number);
    }    
}

But when I run my xhtml code, I see the panel but I do not see anything inside it. Also, the System.out.println() output is not displayed on console. How do I resolve the issue? My basic aim is that when I run the xhtml code then a random number must be shown on the panel.

Upvotes: 0

Views: 106

Answers (1)

Philipp
Philipp

Reputation: 333

The solution is to use a Class named correct according to the Java Code Conventions: http://www.oracle.com/technetwork/java/codeconventions-135099.html

tl;dr: Rename your Class "randomnum" to "Randomnum".

This way JSF will find the Bean, instantiate it, call @PostConstruct and display the value.

Upvotes: 1

Related Questions