Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a java.util.Date in URL from JSF page?

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?

like image 679
user3058397 Avatar asked Dec 25 '22 16:12

user3058397


1 Answers

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>

See also:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
like image 54
BalusC Avatar answered Jan 12 '23 03:01

BalusC