0

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 !

Derbie
  • 413
  • 1
  • 12
  • 28

2 Answers2

0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I've now a problem of type between date from java.sql.Date and java.util.Date by i'm gonna solve it by myself. Working fine, thanks ! – Derbie Dec 23 '12 at 14:06
  • Just make sure that you never have `import java.sql.Date;` throughout your JSF code. This import should only be used in your JDBC code. You should only use `import java.util.Date;` throughout your JSF code. See also e.g. http://stackoverflow.com/questions/11079505/converting-string-to-sql-date-type/11083385#11083385 and http://stackoverflow.com/questions/3323618/handling-mysql-datetimes-and-timestamps-in-java/3323870#3323870 – BalusC Dec 23 '12 at 14:09
0

You need to update/define default values at the bean. In this case you can define the value in the constructor; if the value depends of injected attributes you need to use a @PostConstruct method.

SJuan76
  • 24,532
  • 6
  • 47
  • 87