开发者

How does this work? Java Date, Calendar and Richface question

I have noticed that numerous Date methods in Java have been deprecated, but rather most people tell me to use Calender.

Here is my problem. I am trying to create a Calendar where you can select a date and within Java set the date. So I have

public class someClass
{

    private Date startDate;
    private Date endDate;

    public void setStartDate(Date date) 
    {
        this.startDate = date;
    }

    public void setEndDate(Date date) 
    {
        this.endDate = date;
    }   

    public Date getStartDate()
    {
        return this.startDate;
    }

    public Date getEndDate() 
    {
        return this.end;
    }
开发者_如何学运维
}

with the RichFaces code

<rich:calendar popup="true" mode="ajax"  value="#{someClass.startDate}"/>

When I manually set it up in code it seems to show up, but when I change to a different date it never sets the date.

I have also seen that within calendars they tell me to use Calendar. But when I type in Calenders.Time. it's not in Java, is it because it's protected? How does it work with RichFaces? Can RichFaces access it?


As to the concrete question of "How to use java.util.Calendar with <rich:calendar>?", just bind the component to Calendar#getTime() which in turn returns a fullworthy java.util.Date.

E.g.

public class Bean {

    private Calendar startDate;

    public Bean() {
        startDate = Calendar.getInstance(); // You need to prepopulate it.
    }

    // ...
}

with

 <rich:calendar value="#{bean.startDate.time}" />

But that will not solve the real problem of the value not being set in the model. This is caused by something else. Don't get mislead by the deprecated methods of Date. Using Date in <rich:calendar> is perfectly fine. You only shouldn't be using those deprecated methods to set the year/month/day/etc of the date programmatically yourself like as in bean's constructor.

Your real problem can have many causes. Perhaps the <rich:calendar> isn't been placed in a <h:form>? Perhaps you're nesting multiple <h:form> components? Perhaps you've somewhere an immediate="true"? Perhaps you're grabbing/debugging the value at the wrong moment? Etc..etc..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜