Search code examples
jsfnetbeansprimefacesmanaged-bean

How to set jsf component properties from managedBean?


As said in the title, i'd like to change a component property identified by an id in a jsf page from a managed bean. Here is my jsf code :

<p:calendar value="#{eventBean.beginDate}" id="from" pattern="dd/MM/yyyy HH:mm" required="true"/>

It's a PrimeFaces component. At the initialization of the page, i've got an empty field that display by clicking in a calendar. Choosing a date fill the field with the selected value. My question is : how to fill the field with the current date at the initialization of my jsf page ? I dunno if there is a possibility by using PrimeFaces calendar component properties (i've try several things that didn't work) and i'd like to know if that's possible using managed bean.

Thank you !


Solution

  • Just set the property during bean's (post)construction.

    private Date beginDate;
    
    public EventBean() {
        // Here, in constructor.
        eventDate = new Date();
    }
    
    @PostConstruct
    public void init() {
        // Or here, in a @PostConstruct method. 
        // This is invoked after any dependency and managed property injection.
        eventDate = new Date();
    }
    

    Note that this approach is not specific to the calendar property. It applies to all kinds of properties.