问题
I would like to use the new HTML5 <input type="date"> and bind its value to a managed bean:
<input type="date" value="#{bean.date}"/>
I want to do this, because I like this more than the one offered by PrimeFaces.
How can I achieve this?
回答1:
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}" />
If you're not on JSF 2.2 yet, you could get away with OmniFaces' Html5RenderKit. This allows you to use new HTML5 attributes on among others <h:inputText>.
<h:inputText type="date" value="#{bean.date}" />
See also
- Custom HTML tag attributes are not rendered by JSF
- HTML5 friendly markup in JSF 2.2
- OmniFaces Html5RenderKit showcase page
回答2:
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".
来源:https://stackoverflow.com/questions/13484747/is-it-possible-to-bind-an-html5-component-input-type-date-to-a-managed-be