0

I use this questions to create a composite component with the behaviour of a time selector.

This is my composite xhtml

<ui:composition xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:rich="http://richfaces.org/rich">
<cc:interface componentType="customTimeBean">
    <cc:attribute name="date" type="java.util.Date" required="true" />
</cc:interface>
<cc:implementation>
    <rich:inputNumberSpinner value="#{cc.hours}" minValue="0"
        maxValue="23" />
    <h:outputLabel value=":" />
    <rich:inputNumberSpinner value="#{cc.minutes}" minValue="0"
        maxValue="59" />
</cc:implementation>
</ui:composition>

This is my Faces Component

@FacesComponent(value = "customTimeBean")
public class CustomTimeBean extends UINamingContainer {

    private Date getDate() {

        Date d = (Date) getAttributes().get("date");
        if (d == null) {
            //throw new RuntimeException("Date no debe ser nulo");
            d = new date();
        }
        return d;
    }

    public void setMinutes(int value) {

        getDate().setMinutes(value);
    }

    public void setHours(int value) {

        getDate().setHours(value);
    }

    public int getMinutes() {

        return getDate().getMinutes();
    }

    public int getHours() {

        return getDate().getHours();
    }

    public void setSeconds(int value) {

        getDate().setHours(value);
    }

    public int getSeconds() {

        return getDate().getSeconds();
    }

}

And the usage

<sigh:time date="#{bean.date}" hasSeconds="false"/>

My test case:

<h:outputLabel value="#{controller.date}" id="date" />
<sigh:time date="#{controller.date}" />
<a4j:commandButton render="date" />

The "controller":

@ManagedBean
public class Controller  {

    Date date;

    public Date getDate() {

        return date;
    }

    public void setDate(Date date) {

        this.date = date;
    }
}     

This works well, but, this doesn't work when I pass a null Date as attribute, how can I update the value in the Bean (controller.date)?

In my test case, when I press the a4j:commandButton and date in the controller is null, the outputlabel dont show nothing (date is null), when the date is not null, the date is update everytime I click the Button.

Sorry for my bad english.

Thanks!

Community
  • 1
  • 1
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40

1 Answers1

0

Just replace

throw new RuntimeException("Date no debe ser nulo");

by

d = new Date();

Unrelated to the concrete problem, the Date methods which you're using there are deprecated. See also the javadoc. The main reason is that it doesn't deal with timezones/DST properly. You should internally in the composite be using java.util.Calendar instead.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi @BalusC, first, Thanks for the reply, i made a application with your answers!. My first solution is like you propose, but in the bean (bean.date) the date is not update!, I'll update my question with your suggestions and my test case. – Arturo Volpe Dec 27 '12 at 14:35