14

I would like to use the HTML <input type="date"> input type and bind its value to a managed bean:

<input type="date" value="#{bean.date}"/>

How can I achieve this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Pigritia
  • 311
  • 3
  • 10
  • 1
    That is not a jsf component, that is just HTML and you cannot bind values to it unless you create your own component but that is not recommendable if you are not experienced in jsf. – ElderMael Nov 21 '12 at 01:25

2 Answers2

22

This is only possible since JSF 2.2. This feature is known as "passthrough elements".

<html xmlns:jsf="http://xmlns.jcp.org/jsf">
...
<input type="date" jsf:value="#{bean.date}" />

Alternatively, use "passthrough attributes".

<html xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<h:inputText a:type="date" value="#{bean.date}" />

In older JSF versions, use a custom component and/or renderer. You can find links to examples in Custom HTML tag attributes are not rendered by JSF.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • With JSF2.2 using the jsf:value does not render correctly. The final HTML is render exactly with a jsf:value attribute - `` – Mateus Viccari Jul 15 '16 at 13:42
  • This is odd, i already have JSF version 2.2.8 in pom.xml (org.glassfish / javax.faces / 2.2.8) – Mateus Viccari Jul 15 '16 at 14:09
  • @MateusViccari JSF requires `type` attribute for conversion to JSF elements to work: `` – Ivan Jan 05 '18 at 07:53
  • hello, I just applied `` to my code and it binds the bean as expected but, is it possible, keeping this mechanism in place, to display the current date (or any date for that matter) in the input before the form is submitted? thanks – Scaramouche Mar 27 '19 at 22:32
3

Another way (works only with JSF 2.2) is to use the f:passThroughAttribute inside your inputText:

<h:inputText id="yourNumberField" value="#{mainController.myBeautifulNumber}">
    <f:passThroughAttribute name="type" value="number"/>
    <f:passThroughAttribute name="step" value="0.02"/>
</h:inputText>

The f: namespace is the default xmlns:f="http://xmlns.jcp.org/jsf/core".

Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101