Is It Possible to Bind An HTML5 Component ( <input type=“date”>) To A Managed Bean Property?

纵然是瞬间 提交于 2019-12-12 07:33:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!