4

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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user3058397
  • 103
  • 2
  • 8
  • *As a side note*: the part of URL that contains data to be passed is called a [**query string**](http://en.wikipedia.org/wiki/Query_string). – informatik01 Dec 13 '13 at 19:40

1 Answers1

10

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yes I got the date as util.Date from JSF inputText and converted it first to String and than back to Date for database queries. But the tip with the DateTime converter for viewParams is good. Thank you! – user3058397 Dec 17 '13 at 01:55