JSF自定义简单输出组件

作者在 2008-01-31 13:30:58 发布以下内容
        JSF Tutorial上的,觉得很清楚,拿来分享下。
        首先,JSF是支持自定义组件的。一共有三类自定义组件:1.仅输出组件;2.接受属性的组件;3.接受输入的组件。今天来讲下第一个:仅输出组件(Output-only)。
        大致分为以下几步:
        1.自定义的组件类通常要继承UIComponentBase类或其他组件类;
        2.用标签将组件与使用名联系起来;
        3.创建TLD文件声明该组件;
        4.在faces-config.xml中,给组件类命名;
        5.在jsp页面中引入并使用标签。
        下面就详细介绍如何使用自定义组件的。这个例子就是在页面上使用自定义的输出标签显示出当前日期与时间。
        首先,写自定义组件类HtmlSimpleDate.java。该类的作用就是输出一个日期时间。

package src;

import javax.faces.component.*;

import javax.faces.context.*;

import java.io.*;

import java.util.*;

public class HtmlSimpleDate extends UIComponentBase {

public void encodeBegin(FacesContext context) throws IOException {

ResponseWriter ut = context.getResponseWriter();

Calendar currentDateTime = new GregorianCalendar();

String utput = String.format("It is now %tr on %tD.", currentDateTime, currentDateTime);

out.write(output);

}

public String getFamily() {

return null;

}

}

        这里要注意的是,如果要输出HTML内容,通常在类名前面加上"HTML"。继承UIComponentBase类后,要覆盖encodeBegin或者encodeEnd方法,同时必须覆盖getFamily方法。如果该组件要接受输入,就覆盖encodeEnd方法。
如果没有单独的组件呈现者,则getFamily方法中只要return null就可以了(这里还不是太明白,先照着写)。
        然后,写该组件的标签类HtmlSimpleDateTag.java。继承UIComponentTag类,覆盖getComponentType和getRendererType方法。

package src;

import javax.faces.webapp.*;

public class HtmlSimpleDateTag extends UIComponentTag {

public String getComponentType() {

return "HtmlSimpleDate";

}

public String getRendererType() {

return null;

}

}
        getComponentType方法中,return的内容要与在faces-config.xml中的配置内容一致。仍然,如果没有单独的呈现者,getRendererType方法返回null。
        然后是在tld文件中声明你的标签。通常,该tld文件放在WEB-INF/tlds下。
        HtmlSimpleDate.tld:

<?xml version="1.0" encoding="UTF-8"?>

<taglib version="2.0">

<tlib-version>1.0</tlib-version>

<short-name>jsf-custom-components</short-name>

<uri>http://coreservlets.com/jsf/simple</uri>

<tag>

<description>

Output Date in server's timezone

</description>

<name>simpleDate</name>

<tag-class>src.HtmlSimpleDateTag</tag-class>

<body-content>empty</body-content>

<attribute><name>id</name></attribute>

<attribute><name>rendered</name></attribute>

</tag>

</taglib>
        <uri>里面的是你在jsp中要引入时唯一能够辨别的地址,不一定是真实存在的。其实这个uri就是统一资源标示符的意思。如果在这个自定义标签中需要用到某些属性,如id, rendered的话,则在该tld文件中定义。
        然后,在faces-config.xml声明你的组件名和组件类:
       

<component>

<component-type>HtmlSimpleDate</component-type>

<component-class>src.HtmlSimpleDate</component-class>

</component>
        注意,<component-type>要与之前组件标签类中getCompenentType返回的内容一致。
        然后就是在你的jsp页面中引入并使用标签了。
        htmlSimpleDate.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<%@taglib uri="http://coreservlets.com/jsf/simple" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Html Simple Date Tag</title>

</head>

<body>

<f:view>

<h1><c:simpleDate/></h1>

</f:view>

</body>

</html>
        我这里将标签的前缀命名为c,simpleDate则是在tld文件中命名的。同时要注意一定要在<f:view>标签对里面才能正常显示。

默认分类 | 阅读 2476 次
文章评论,共0条
游客请输入验证码