简体   繁体   中英

Strange behavior of convertDateTime in JSF

I am using JSF with richfaces. I am using convertDateTime of core ip to convert an inputText to Date.

<h:inputText styleClass="datepicker" value="#{searchBean.creationDate}" id="inputfield">
  <f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>

Where creationDate in SearchBean is of type Java util Date and I am using a jquery datpicker to populate the input field.

The problem is that it is not working fine. When I submit form without entering any date (empty input field), everything works fine. But, when I select the date, the call doesn't reach bean (tested it in debug mode). The page just refreshes itself without any exception/logging on the server side.

I have implemented a quick LifeCycleListener which showed following output:

With input field filled with date:

START PHASE RESTORE_VIEW(1)
END PHASE RESTORE_VIEW(1)
START PHASE APPLY_REQUEST_VALUES(2)
END PHASE APPLY_REQUEST_VALUES(2)
START PHASE PROCESS_VALIDATIONS(3)
END PHASE PROCESS_VALIDATIONS(3)
START PHASE UPDATE_MODEL_VALUES(4)
END PHASE UPDATE_MODEL_VALUES(4)
START PHASE INVOKE_APPLICATION(5)
END PHASE INVOKE_APPLICATION(5)
START PHASE RENDER_RESPONSE(6)
END PHASE RENDER_RESPONSE(6)

With empty date :

START PHASE RESTORE_VIEW(1)
END PHASE RESTORE_VIEW(1)
START PHASE APPLY_REQUEST_VALUES(2)
END PHASE APPLY_REQUEST_VALUES(2)
START PHASE PROCESS_VALIDATIONS(3)
END PHASE PROCESS_VALIDATIONS(3)
START PHASE RENDER_RESPONSE(6)
END PHASE RENDER_RESPONSE(6)

It is clear that there are two steps missing in second case. I am very new to JSF and don't have much idea about internal working of it. Moreover, there are no validation/conversion exceptions. I am stuck here and I have no idea where to look for the solution. Any help is appreciated.

Reason for update model and invoke application not shown when you dont enter date is because your validation has failed, in case of validation failure model is not updated with UI content and application logic (your custom code) will not be invoked.

If you have added error messages in your .properties file then add the following

javax.faces.converter.DateTimeConverter.DATE=value ({0}) must be a date

Else

<h:inputText styleClass="datepicker" value="#{searchBean.creationDate}" id="inputfield" converterMessage="Please Enter creation date in yyyy-MM-dd format">
  <f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>

This would enable to show error message when validation fails.

You will be getting conversion error when you submit null as date.

If you're using JSF 1.2 and RichFaces 3.x then use the rich:calendar tag component to work with dates, it uses jquery behind the scenes to get displayed. Using it in your example:

<rich:calendar id="calCreationDate" popup="true"
    value="#{searchBean.creationDate}"
    enableManualInput="true"
    locale="#{application.locale}"
    datePattern="#{application.datePattern}"
    showApplyButton="false" />

In my application:

public class ApplicationBean {
    private Locale locale;
    private String datePattern;

    public ApplicationBean() {
        locale = new Locale("es"); //for spanish
        datePattern = "dd/MM/yyyy"; //you can change it to yyyy-MM-dd
    }
}

Where ApplicationBean has ApplicationScope.

More info here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM