jsf2.3 html5,JSF 2.2: HTML5 Support

本文介绍了如何在JSF2.2中融入HTML5元素支持,包括新引入的<factory>和<render-kit-factory>,以及如何使用p:passThroughAttributes为input元素添加HTML5特性,如类型属性和客户端验证。展示了如何使用新的Facelet标签库来实现更现代的表单设计,同时讨论了浏览器兼容性和实际应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#JSF 2.2: HTML5 Support#

No doubt HTML 5 is the hottest topic in web application development.

JSF 2.0 is not ready for HTML 5 support. When you are using JSF 2.0/Java EE 6, you can use omnifaces to add HTML5 support.

omnifaces included a Html5RenderKitFactory to render HTML 5 elements.

 <factory> <render-kit-factory>org.omnifaces.renderkit.Html5RenderKitFactory</render-kit-factory> </factory> 

In Java EE 7, HTML 5 is the first class citizen. But refactoring all components in JSF is not a smart decision. JSF 2.2 provided a compromise solution to support HTML 5.

In HTML5, form input elements are improved, the type attribute can be text, email, number, range, url, date etc. Ideally, browser should provide client validation for these input elements.

An example for JSF 2.2 and HTML 5 support.

 <h:form> <h:messages showDetail="false" showSummary="true"/> Text: <h:inputText id="text" value="#{html5Bean.text}" required="true"> <f:passThroughAttribute name="placeholder" value="Type text here..."/> <f:passThroughAttribute name="required" value="true"/> </h:inputText> <br/> Url: <h:inputText id="url" value="#{html5Bean.url}"> <f:passThroughAttribute name="type" value="url"/> </h:inputText><br/> Email: <h:inputText id="email" p:type="email" value="#{html5Bean.email}" /><br/> Number: <h:inputText id="number" p:type="number" p:min="1" p:max="10" value="#{html5Bean.number}" > <f:convertNumber minFractionDigits="0"/> </h:inputText> <br/> Range: <h:inputText id="range" value="#{html5Bean.range}"> <!-- f:passThroughAttributes value="#{html5Bean.attrs}"/ --> <f:passThroughAttributes value="#{{'type':'range', 'min':'0', 'max':'10', 'step':'2'}}"/> <f:convertNumber minFractionDigits="0"/> </h:inputText><br/> Date: <h:inputText id="date" p:type="date" value="#{html5Bean.date}" > <f:convertDateTime pattern="yyyy-MM-dd"/> </h:inputText><br/> <h:commandButton value="Save" action="#{html5Bean.submit()}"> <f:ajax execute="@form" render="@all"/> </h:commandButton> </h:form> <h:panelGroup id="out"> Text: <h:outputText value="#{html5Bean.text}"/><br/> Url: <h:outputText value="#{html5Bean.url}"/><br/> Email: <h:outputText value="#{html5Bean.email}"/><br/> Number: <h:outputText value="#{html5Bean.number}"/><br/> Range: <h:outputText value="#{html5Bean.range}"/><br/> Date: <h:outputText value="#{html5Bean.date}"/><br/> </h:panelGroup> 

JSF 2.2 provides a new facelets taglib named passthrough to process the new attributes added in HTML 5.

 xmlns:p="http://xmlns.jcp.org/jsf/passthrough" 

For example, you want to add placeholder to input element, just added a p:placeholder attribute.

 <h:inputText p:placeholder="Type text here..."/> 

This tell JSF HTMLRenderKit keep back the attribute placeholder directly, not like JSF 2.0, any none support attributes will be ate by HTML RenderKit.

The core taglib also added a new tag to support passthrough feature.

 <f:passThroughAttribute name="placeholder" value="Type text here..."/> 

This is equivalent to the above version.

You can add more than one attributes at the same time.

 <h:inputText id="number" p:type="number" p:min="1" p:max="10" value="..."/> 

Or use multi f:passThroughAttribute nested in the inputText component.

 <h:inputText id="text" value="#{html5Bean.text}" required="true"> <f:passThroughAttribute name="placeholder" value="Type text here..."/> <f:passThroughAttribute name="required" value="true"/> </h:inputText> 

Or use a f:passThroughAttributes nested in the inputText component, it can accept a Map.

 <f:passThroughAttributes value="#{html5Bean.attrs}"/> 

In backend bean, a Map is declared.

 private Map<String, String> attrs = new HashMap<String, String>(); @PostConstruct public void init() { log.info(" call init@"); this.attrs.put("type", "range"); this.attrs.put("min", "1"); this.attrs.put("max", "10"); this.attrs.put("step", "2"); } 

You can also use a EL 3.0 Map expression as value. EL 3.0 is extracted from JSF specifcation as a standalone specification now, we will discuss it in further post.

 <f:passThroughAttributes value="#{{'type':'range', 'min':'0', 'max':'10', 'step':'2'}}"/> 

Through the passthrough feature in JSF 2.2, you can add any custom attribute to input element, such as the data-XXX attribute in Bootstrap framework.

Currently only Opera supports all elements in this post.

The following is the result of HTML 5 required attribute valuation.

jsf-html5-required-opera.png

This is the the whole form displayed in Opera.

03321a59bedce295d21709e611ec750e.png

In Firefox, the range, number, date are not rendered, plain input text instead.

HTML 5 is beautiful, but the browser support is not good as expected.

Check out the complete codes from my github.com, and play it yourself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值