Reputation: 517
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?
Upvotes: 11
Views: 985
Reputation: 1109715
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 wheneverf: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);
}
}
// ...
}
Upvotes: 3
Reputation: 516
I'm guessing you have some text displayed based on a button click (correct me if I'm wrong). So it should be something like that:
<h:commandButton value="Convert" action="#{bean.doBtnConvert}" />
<h:commandButton value="Don't convert" action="#{bean.doBtnDontConvert}" />
<h:panelGroup id="pgText">
<h:outputText value="#{bean.someDateTime}" rendered="#{bean.convert}">
<f:convertDateTime pattern="dd.MM.yyyy HH:mm:ss" />
</h:outputText>
<h:outputText value="#{bean.someDateTime}" rendered="#{not bean.convert}"> />
</h:panelGroup>
And in bean you have the following field and methods:
private Date someDate;
private boolean convert;
public String doBtnConvert(){
setConvert(true);
String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
return viewId + "?faces-redirect=true";
}
public String doBtnDontConvert(){
setConvert(false);
String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
return viewId + "?faces-redirect=true";
}
// getter and setter for 'someDate' and 'convert' fields
Upvotes: 0