JSF2自定义组件编程系列 第五部分(一)

2014-11-23 23:41:19 · 作者: · 浏览: 2

在写这一章的时候,没有想到遇到很多的困难。现在简单的说一下:
1.添加taglib.xml文件里面的namespace看上去很美,但是带来了很大的困扰―EL表达式失效。这是我和另一位程序员在java.net上的帖子。
http://www.java.net/forum/topic/glassfish/glassfish-webtier/el-composite-component-taglib-jsf20
目前我的解决方案是绕过这个问题,只采用标准namespace,也就是http://java.sun.com/jsf/composite/tag_folder_path。同时我简单搜索了一下PrimeFaces的源代码,发现并没有采用Composite Component的方式实现,因为没有找到任何xhtml文件。

2.<>一书的问题
a.第11章333页,
public class loginPanel extends UINamingContainer
这行代码根本是错的,正确的应该是
public class loginPanel extends UIInput implements UINamingContainer
b.该书没有提供例子演示如何将一个拥有Backing class的Composite Component打包进一个jar包中,因此书中提供的代码片段不可信
c.xhtml中cc的别名和默认cc同名,混淆读者的概念

http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">




requiredMessage="Userid is required" id="userid" />


requiredMessage="Password is required" id="password" />

rendered="#{! empty cc.facets.loginOutcomeChoiceList}"/>




上面的代码中,xmlns:cc="http://java.sun.com/jsf/composite 是容易误导人的,一般建议使用xmlns:composite而不是xmlns:cc。因为在后面#{cc.}的语法中,cc是预定义的Java对象,代表这个Composite Component的顶层对象NamingContainer

不少问题直到看了<>第三版,才明白。

好,现在开始。
首先实现htmlinput2.xhtml代码:
< xml version="1.0" encoding="UTF-8" >
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:composite="http://java.sun.com/jsf/composite">









composite:interface指定了componentType,JSF runtime用它在在faces-config.xml文件中查找到HtmleInput2类,该类实现了NamingContainer接口,继承了 UIInput类。JSF runtime会创建该类,将之作为Customized component的顶层对象。

HtmlInput2
com.freebird.component.HtmlInput2

如果不指定componentType,JSF runtime会根据xhtml文件的名称htmlinput2去查找htmlinput2.java,如果找到,并且该类也实现了 NamingContainer接口,继承了UIInput类,则创建该类,将之作为Customized component的顶层对象。
如果还找不到,JSF runtime会创建NamingContainer接口的默认实现类作为顶层对象。
什么时候我们应该指定compnentType呢?当你想有一些java代码后台帮助tag处理一些行为的时候。
其他的部分很简单,建议参考<>第三版第九章。

现在来看一下HtmleInput2类的实现:
package com.freebird.component;

import javax.faces.component.NamingContainer;
import javax.faces.component.UIOutput;
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import javax.faces.component.FacesComponent;
import javax.faces.event.ActionListener;
import javax.faces.convert.ConverterException;
import javax.faces.context.FacesContext;
import java.io.IOException;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.enterprise.context.ApplicationScoped;
import java.util.Map;

/**
* Describe class HtmlInput2 here.
*
*
* Created: Sat Jan 1 16:08:53 2011
*
* @author chenshumailto:chenshu@csdesktop">chenshu
* @version 1.0
/
public class HtmlInput2 extends UIInput implements NamingContainer{

public HtmlInput2(){
getLogger().info("HtmlInput2 constructor");
}

@Override
public String getFamily() {
return "javax.faces.NamingContainer";
}

private Logger getLogger(){
return Logger.getLogger(HtmlInput2.class.getName());
}

public void print(ActionEvent event){
getLogger().info("enter print method");
}

public