Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable enable jsf f:convertDateTime

Tags:

jsf

converter

I have two buttons, in one I need <f:convertDateTime> to work but in another I need to disable <f:convertDateTime> on button click.

I tried the attributes rendered and disabled, but it didn't work, which was my mistake as it is not available as per the API docs.

Also, is there a way to override the class javax.faces.converter.DateTimeConverter such that whenever f:convertDateTime is triggered my class will be called?

like image 437
Anupam Avatar asked Aug 18 '16 05:08

Anupam


1 Answers

I tried the attributes rendered and disabled, but it didn't work, which was my mistake as it is not available as per the API docs.

Indeed, this behavior is not supported. However, as to a possible solution, you basically already gave the answer yourself:

Also, is there a way to override the class javax.faces.converter.DateTimeConverter such that whenever f:convertDateTime is triggered my class will be called?

That is possible and will also solve your initial problem. Just register it as <converter> in faces-config.xml on exactly the same <converter-id> as <f:convertDateTime>.

<converter>
    <converter-id>javax.faces.DateTime</converter-id>
    <converter-class>com.example.YourDateTimeConverter</converter-class>
</converter>

Therein you could do additional conditional checking, such as checking if a certain button is pressed, or if a certain request parameter is present or absent. If you'd like to continue the default <f:convertDateTime> job, just delegate to super provided that your converter extends from DateTimeConverter.

E.g. in getAsObject():

public class YourDateTimeConverter extends DateTimeConverter {

    @Override
    public void getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        // ...

        if (yourCondition) {
            // Do your preferred way of conversion here.
            // ...
            return yourConvertedDateTime;
        } else {
            // Do nothing. Just let default f:convertDateTime do its job.
            return super.getAsObject(context, component, submittedValue);
        }
    }

    // ...
}
like image 147
BalusC Avatar answered Nov 18 '22 06:11

BalusC