试着开始开发基于JSF2的Html5组件包

[JSF] JSF & HTML5 高级程序设计 (英文版) ☆ 资源说明:☆ [Apress] JSF & HTML5 高级程序设计 (英文版) [Apress] Pro JSF and HTML5 Building Rich Internet Components (E-Book) ☆ 图书概要:☆ Pro JSF and HTML5 shows you how to leverage the full potential of JavaServer Faces (JSF) and HTML5. This book is for Java developers who aspire to build sophisticated, enterprise-grade web experiences with HTML5-enabled JSF. Written by JSF experts and verified by established community figures, this book will serve as your primary resource, helping you build or integrate well-designed HTML5-enabled JSF components into your rich internet applications. ☆ 出版信息:☆ [作者信息] Hazem Saleh, Allan Lykke Christensen, Zubin Wadia [出版机构] Apress [出版日期] 2013年11月24日 [图书页数] 412页 [图书语言] 英语 [图书格式] PDF 格式 立即下载

现在许多浏览器都开始支持html5,然而当前的ide编辑环境还不能识别html5的标签,在工作中,有应用html5的需求,想通过开发基于JSF2的Html5组件包,来满足这样的现实需求,实现可复用的html5表现组件。

 

开始试写,第一个标记<h5:doctype/>

 

 

组件包结构如下:

 

 

 

 组件的类文件

 

Java代码
  1. package  com.putesoft.jsf.component.html5;  
  2.   
  3. import  javax.faces.component.UIComponentBase;  
  4.   
  5. /**  
  6.  * 开始于 2010-04-07  
  7.  * html5的DOCTYPE的文档类型声明,一个简单的html5标记封装  
  8.  * 最后修改于 2010-04-09  
  9.  * @author 普特工作室  
  10.  * @version 1.0  
  11.  */   
  12. public   class  Html5Doctype  extends  UIComponentBase {  
  13.   
  14.     /**  
  15.      * 使用组件的类名作为,返回组件的家族标识  
  16.      * @return  
  17.      */   
  18.     @Override   
  19.     public  String getFamily() {  
  20.         return  Html5Doctype. class .getName();  
  21.     }  
  22.   
  23.   
  24.   
  25. }  
package com.putesoft.jsf.component.html5;

import javax.faces.component.UIComponentBase;

/**
 * 开始于 2010-04-07
 * html5的DOCTYPE的文档类型声明,一个简单的html5标记封装
 * 最后修改于 2010-04-09
 * @author 普特工作室
 * @version 1.0
 */
public class Html5Doctype extends UIComponentBase {

    /**
     * 使用组件的类名作为,返回组件的家族标识
     * @return
     */
    @Override
    public String getFamily() {
        return Html5Doctype.class.getName();
    }



}

 

组件的标记处理类

 

Java代码
  1. package  com.putesoft.jsf.taglib.html5;  
  2.   
  3. import  com.putesoft.jsf.component.html5.Html5Doctype;  
  4. import  com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer;  
  5. import  javax.faces.webapp.UIComponentELTag;  
  6.   
  7. /**  
  8.  * 开始于 2010-04-08  
  9.  * 组件的标志处理器类,组件的标记类  
  10.  * 衔接代码,让这个组件能在JSP页面上下文中执行,从而钩到其它的相关类  
  11.  * 最后修改于 2010-04-09  
  12.  * @author 普特工作室  
  13.  * @version 1.0  
  14.  */   
  15. public   class  Html5DoctypeTag  extends  UIComponentELTag {  
  16.   
  17.     /**  
  18.      *   
  19.      * 直接使用类的全路径名,作为组件的类型名,  
  20.      * 此处是衔接代码,通过此衔接代码,  
  21.      * 根据获得的组件名,在faces-config.xml中,找到对应的组件类  
  22.      * 然后执行组件的处理逻辑  
  23.      * @return  
  24.      */   
  25.     @Override   
  26.     public  String getComponentType() {  
  27.         String compontentType = Html5Doctype.class .getName();  
  28.         return  compontentType;  
  29.     }  
  30.   
  31.   
  32.     /**  
  33.      * 直接使用类的全路径名,作为组件的渲染器类型名,  
  34.      * 此处是衔接代码,通过此衔接代码,  
  35.      * 根据获得的渲染器名,在faces-config.xml中,找到对应的渲染器类  
  36.      * 然后执行组件的渲染处理  
  37.      * @return  
  38.      */   
  39.     @Override   
  40.     public  String getRendererType() {  
  41.         String rendererType = Html5DoctypeRenderer.class .getName();  
  42.         return  rendererType;  
  43.     }  
  44.   
  45.   
  46.   
  47. }  
package com.putesoft.jsf.taglib.html5;

import com.putesoft.jsf.component.html5.Html5Doctype;
import com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer;
import javax.faces.webapp.UIComponentELTag;

/**
 * 开始于 2010-04-08
 * 组件的标志处理器类,组件的标记类
 * 衔接代码,让这个组件能在JSP页面上下文中执行,从而钩到其它的相关类
 * 最后修改于 2010-04-09
 * @author 普特工作室
 * @version 1.0
 */
public class Html5DoctypeTag extends UIComponentELTag {

    /**
     * 
     * 直接使用类的全路径名,作为组件的类型名,
     * 此处是衔接代码,通过此衔接代码,
     * 根据获得的组件名,在faces-config.xml中,找到对应的组件类
     * 然后执行组件的处理逻辑
     * @return
     */
    @Override
    public String getComponentType() {
        String compontentType = Html5Doctype.class.getName();
        return compontentType;
    }


    /**
     * 直接使用类的全路径名,作为组件的渲染器类型名,
     * 此处是衔接代码,通过此衔接代码,
     * 根据获得的渲染器名,在faces-config.xml中,找到对应的渲染器类
     * 然后执行组件的渲染处理
     * @return
     */
    @Override
    public String getRendererType() {
        String rendererType = Html5DoctypeRenderer.class.getName();
        return rendererType;
    }



}

 

组件渲染器类

 

Java代码
  1. package  com.putesoft.jsf.renderkit.html5;  
  2.   
  3. import  java.io.IOException;  
  4. import  javax.faces.component.UIComponent;  
  5. import  javax.faces.context.FacesContext;  
  6. import  javax.faces.context.ResponseWriter;  
  7. import  javax.faces.render.Renderer;  
  8.   
  9. /**  
  10.  * 开始于 2010-04-10  
  11.  * HTML5的文档类型渲染器  
  12.  * 最后修改于 2010-04-10  
  13.  * @author 普特工作室  
  14.  * @version 1.0  
  15.  */   
  16. public   class  Html5DoctypeRenderer  extends  Renderer {  
  17.   
  18.   
  19.     /**  
  20.      * 编码组件,渲染成Html代码  
  21.      * @param context  
  22.      * @param component  
  23.      * @throws IOException  
  24.      */   
  25.     @Override   
  26.     public   void  encodeBegin(FacesContext context, UIComponent component)  throws  IOException {  
  27.         //从faces上下文环境中获得响应书写器   
  28.         ResponseWriter writer = context.getResponseWriter();  
  29.         //直接把字符串写出,不进行字符转义处理   
  30.         writer.write("<!DOCTYPE HTML>" );  
  31.     }  
  32.   
  33.   
  34.   
  35.   
  36. }  
package com.putesoft.jsf.renderkit.html5;

import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

/**
 * 开始于 2010-04-10
 * HTML5的文档类型渲染器
 * 最后修改于 2010-04-10
 * @author 普特工作室
 * @version 1.0
 */
public class Html5DoctypeRenderer extends Renderer {


    /**
     * 编码组件,渲染成Html代码
     * @param context
     * @param component
     * @throws IOException
     */
    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        //从faces上下文环境中获得响应书写器
        ResponseWriter writer = context.getResponseWriter();
        //直接把字符串写出,不进行字符转义处理
        writer.write("<!DOCTYPE HTML>");
    }




}
 

标记库文件

 

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < taglib   
  3.     version = "2.1"   
  4.     xmlns = "http://java.sun.com/xml/ns/javaee"   
  5.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  6.     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" >   
  7.       
  8.       
  9.     < description > 用于JSF2的,html5通用标记的JSF封装 </ description >   
  10.     < tlib-version > 1.0 </ tlib-version >   
  11.     < short-name > jsf_html5 </ short-name >   
  12.     < uri > http://putesoft.com/jsf_html5 </ uri >   
  13.       
  14.       
  15.     < tag >   
  16.         < description > Html5的文档类型声明封装 </ description >   
  17.         < name > doctype </ name >   
  18.         < tag-class >   
  19.             com.putesoft.jsf.taglib.html5.Html5DoctypeTag  
  20.         </ tag-class >   
  21.     </ tag >   
  22.       
  23. </ taglib >   

 

 

组件库文件

 

Java代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2.   
  3. <!--  
  4.     Document   : faces-config.xml  
  5.     Created on : 201048 日, 下午 5 : 09   
  6.     Author     : 普特工作室  
  7.     Description: JSF组件库的组件注册  
  8. -->  
  9.   
  10. <faces-config  
  11.     xmlns="http://java.sun.com/xml/ns/javaee"   
  12.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  13.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"   
  14.     version="2.0" >  
  15.   
  16.   
  17.   
  18.     <component>  
  19.         <description>Html5的文档类型声明DOCTYPE</description>  
  20.         <component-type>  
  21.             com.putesoft.jsf.component.html5.Html5Doctype  
  22.         </component-type>  
  23.         <component-class >  
  24.             com.putesoft.jsf.component.html5.Html5Doctype  
  25.         </component-class >  
  26.     </component>  
  27.   
  28.   
  29.     <render-kit>  
  30.         <renderer>  
  31.             <component-family>  
  32.                 com.putesoft.jsf.component.html5.Html5Doctype  
  33.             </component-family>  
  34.             <renderer-type>  
  35.                 com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer  
  36.             </renderer-type>  
  37.             <renderer-class >  
  38.                 com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer  
  39.             </renderer-class >  
  40.         </renderer>  
  41.     </render-kit>  
  42.   
  43.   
  44. </faces-config>  
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : faces-config.xml
    Created on : 2010年4月8日, 下午5:09
    Author     : 普特工作室
    Description: JSF组件库的组件注册
-->

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">



    <component>
        <description>Html5的文档类型声明DOCTYPE</description>
        <component-type>
            com.putesoft.jsf.component.html5.Html5Doctype
        </component-type>
        <component-class>
            com.putesoft.jsf.component.html5.Html5Doctype
        </component-class>
    </component>


    <render-kit>
        <renderer>
            <component-family>
                com.putesoft.jsf.component.html5.Html5Doctype
            </component-family>
            <renderer-type>
                com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer
            </renderer-type>
            <renderer-class>
                com.putesoft.jsf.renderkit.html5.Html5DoctypeRenderer
            </renderer-class>
        </renderer>
    </render-kit>


</faces-config>

 

组件在Jsp文件中使用

 

Html代码
  1. < %@page  contentType = "text/html"   pageEncoding = "UTF-8" % >   
  2.   
  3. < %@taglib  prefix = "f"   uri = "http://java.sun.com/jsf/core" % >   
  4. < %@taglib  prefix = "h"   uri = "http://java.sun.com/jsf/html" % >   
  5. < %@taglib  prefix = "h5"   uri = "http://putesoft.com/jsf_html5"  % >   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  8.     "http://www.w3.org/TR/html4/loose.dtd">   
  9.   
  10. < %--  
  11.     This file is an entry point for JavaServer Faces application.  
  12. --%>   
  13. < f:view >   
  14.     < h5:doctype />   
  15.     < html >   
  16.         < head >   
  17.             < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" />   
  18.             < title > JSP Page </ title >   
  19.         </ head >   
  20.         < body >   
  21.             < h1 > < h:outputText   value = "JavaServer Faces" /> </ h1 >   
  22.         </ body >   
  23.     </ html >   
  24. </ f:view >   
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="h5" uri="http://putesoft.com/jsf_html5" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%--
    This file is an entry point for JavaServer Faces application.
--%>
<f:view>
    <h5:doctype/>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>JSP Page</title>
        </head>
        <body>
            <h1><h:outputText value="JavaServer Faces"/></h1>
        </body>
    </html>
</f:view>

 

在tomcat6.0.20上部署运行,成功。

JSF 2.2HTML5 虽然只是小版本的升级,但对于希望在JSF应用中使用HTML5技术的开发人员而言,JSF 2.2带来的更新很重要,尤其是pass through能力,它允许在JSF组件不知情的情况下传递HTML属性。\HTML5中增加了很多新特性,其中有些是在已有的元素上增加了对新属性的支持。例如,input元素的type属性支持text、search、email、url、tel、range、number和date... 阅读详情

相关推荐

JSF----------基础知识初解

初次学习JSF,对其基础进行了一些学习与整理。 JSF(JavaServer Faces)它是一个基于服务器端组件的用户界面框架。 它用于开发Web应用程序。 它提供了一个定义良好的编程模型,由丰富的API和标签库组成。最新版本JSF 2使用Facelets作为其默认模板系统。 它是用Java编写的。 JSF API提供组件(inputText,commandButton等)并帮助管理其状态。...

black-ant 3万+

JSF入门教程

JSF入门教程 1.入门 1.1 简介JSF 1.2 第一个JSF程序 1.3 简单的导航 Navigation 1.4 导航规则设置 1.5 JSF Expression Language 1.6 国际化信息 2. Managed Bea

nengyu的专栏 1万+

html5 jsf 整合,是否可以将JSF + Facelets与HTML 4/5一起使用?

小编典典由于Facelets是一种基于XML的视图技术,本质上可以吃掉并发出XML标记,因此您不能将其与HTML4文档类型一起使用。所述HTML4DOCTYPE描述了几种元件,其不能自封闭,如,,和。然而,随着XML你不得不关闭它们像,等于是使用HTML4DOCTYPE是绝对不能用于Facelets的一个选项(也就是,当你尊重的标准和/或恐惧W3验证,它将但是很好地工作在大多数(如果不是全部)网络...

weixin_42293447的博客 154

JSF入门

最近参与公司的其他项目,但所采用的是JSF。所以就对JSF进行了一下学习。故整理一下。也当做个笔记。 简介JSFWeb应用程序的开发与传统的单机程序开发在本质上存在着太多的差异,Web应用程序开发人员至今不可避免的必须处理HTTP的细节,而HTTP无状态的(stateless)本质,与传统应用程序必须维持程序运行过程中的信息有明显的违背,再则Web应用程序面对网站上不同的使用者同时的存

小崔的博客 1565

MyEclipse10.0

好久没用MyEclipse了,刚刚发布了MyEclipse 10,又正好要写点代码,所以试着在本机上装了装。   我装上了,还没感受到有哪些好用,就是感觉体积庞大,和IBM 的WID一样,是个多面手,啥事都能干,其实经常能使用的也就其中那么几个功能,要是能像插件一样,即插即用就好了。   MyEclipse 10使用最高级的桌面和Web开发技术,包括 HTML5 和 Java EE 6,支持

涂作权的博客 2032

了解 Tapestry,第 1 部分

级别: 初级Brett McLaughlin ,作者和编辑,OReilly Media, Inc.2006 年 206 日如果想出售产品,那么拥有在线设施是很重要的,不论是运作一家数百万美元的公司,还是只是想在假日处理几千个线手镯。在某些情况下,修补一些 Web 页面,并采用一些像 PayPal 或 eBay 这样的预先打包好的支持系统处理销售事务,也可以做得足够好。但是一个全

行舟 1751

html里ie兼容性视图,Internet Explorer - 如何在IE中禁用兼容性视图

Internet Explorer - 如何在IE中禁用兼容性视图我想知道如何阻止使用IE 8的用户进入兼容模式?我发现这个标签,我认为这迫使人们保持在IE-8模式,但我不太确定,不能检查,因为我有IE 9。如果人们处于IE 9模式,我强迫他们不进入IE 8或IE 7兼容模式?我试着将上面的代码放在我的代码中并转到IE 9 - &gt; 工具 - &gt; 兼容性视图(灰显)但“兼容性视图设置...

weixin_33340674的博客 486
上一篇: 试着开始开发基于JSF2的Html5组件包
下一篇: 身份认证狗,在B/S结构系统中的使用
pute2000
博客等级 码龄20年 7粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值