set current date for <af:inputDate> component
Actual code :
I am not able to set current date for <af:inputDate> component.
There are two date components on my JSPX : From Date and To Date.
These attributes are not VO based but page is VO based.
I did bindings for these two components in bean as dtFrom and dtTo
Sample JSPX code,
<af:inputDate label="From Date" id="fromDate" binding="#{myBean.dtFrom}" value="#{myBean.dtFrom}"/>
<af:inputDate label="To Date" id="toDate" binding="#{myBean.dtTo}" value="#{myBean.dtTo}"/>
Sample code from myBean,
public void setDtFrom(RichInputDate dtFrom) {
this.dtFrom = dtFrom;
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
this.dtFrom.setValue(sdf.format(cal.getTime()));
}
public RichInputDate getDtFrom() {
return dtFrom;
}
public void setDtTo(RichInputDate dtTo) {
this.dtTo = dtTo;
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
this.dtTo.setValue(sdf.format(cal.getTime()));
}
public RichInputDate getDtTo() {
return dtTo;
}
Suggestions :
1. Remove the binding attribute from af:inputDate.
2. Set the value attribute to « #{Bean.date}.
3. Have bean method like this -> public Date getDate() { return new Date(); }
Remove the Rich component from bean as well as the binding attribute.
My requirement is
1. On page load set current date as default date
2. After that user may change that date if he/she wishes to.
Solution :
jspx code
<af:inputDate label="#{finiqwebappuiBundle['myBean.from']}" id="fromDate"
binding="#{myBean.dtFrom}"/>
bean code
public void setDtFrom(RichInputDate dtFrom) {
this.dtFrom = dtFrom;
if(this.dtFrom.getValue()==null || this.dtFrom.getValue().toString().equals("")){
this.dtFrom.setValue(new Date());
}
}
There is no need to remove bindings.The bean code is a setter method of From date binding variable.
Laisser un commentaire