I need to pass a java.util.Date in URL from a backing bean to another Facelet (and bean).
Can anybody tell me if this is somehow possible? I know I can't pass complex objects in URL, but does this include Date too?
Or do I have to convert my inserted date (using JSF tag <h:inputText>) to a String, pass it in URL and then convert it back to date?
Fact: HTTP request parameters are of String type.
So, logically, you've got to convert from Date to String while preparing and writing the HTTP request parameter before sending and convert from String back to Date while reading and parsing the incoming HTTP request parameter.
You're not clear as to how exactly you're preparing the URL, but given that you're obtaining the Date as input by <h:inputText>, I'll assume that you're trying to pass it as a parameter of a redirect URL in a backing bean action method. In that case, use SimpleDateFormat to convert from Date to String.
public String submit() {
String dateString = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
return "newpage.xhtml?faces-redirect=true&date=" + dateString;
}
In the receiving page, newpage.xhtml, you can use <f:viewParam> to set it as a bean property. It uses SimpleDateFormat under the covers, so you can use the same pattern syntax.
<f:metadata>
<f:viewParam name="date" value="#{bean.date}">
<f:convertDateTime pattern="yyyyMMddHHmmss" />
</f:viewParam>
</f:metadata>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With