您的位置:首页 > Web前端 > JavaScript

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

2012-03-09 00:00 441 查看
在写这一章的时候,没有想到遇到很多的困难。现在简单的说一下:
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.<<The Complete Reference-Java Server Faces2.0>>一书的问题
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同名,混淆读者的概念

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface/>
<cc:implementation>
<h:panelGrid columns="3">
<h:outputLabel for="#{cc.clientId}:userid" value="Userid:" />
<h:inputText required="true"
requiredMessage="Userid is required" id="userid" />
<h:message for="#{cc.clientId}:userid" />
<h:outputLabel for="#{cc.clientId}:password" value="Password:" />
<h:inputSecret required="true"
requiredMessage="Password is required" id="password" />
<h:message for="#{cc.clientId}:password" />
<h:outputText value="On Login, Go To:"
rendered="#{! empty cc.facets.loginOutcomeChoiceList}"/>
<h:commandButton id="loginButton" value="Login" />
<h:messages for="#{cc.clientId}" />
</h:panelGrid>
</cc:implementation>
</html>

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

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

好,现在开始。
首先实现htmlinput2.xhtml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="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="HtmlInput2">
<composite:editableValueHolder name="inputField" target="in"/>
<composite:valueHolder name="outputField" target="out"/>
</composite:interface>
<composite:implementation>
<h:inputText id="in" required="true"/>
<h:commandButton id="clickButton" value="Click Me!"/>
<h:outputText id="out"/>
</composite:implementation>
</html>

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

现在来看一下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 <a href="mailto:chenshu@csdesktop">chenshu</a>
* @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 void encodeBegin(FacesContext context) throws IOException {
Map requestMap = context.getExternalContext().getRequestParameterMap();
String clientId = getClientId(context);
getLogger().info("clientId:"+clientId);
String inputValue = (String)requestMap.get(clientId+":in");
HtmlInputText input = (HtmlInputText) findComponent("in");
if(null == input){
getLogger().info("can't find input component instance");
super.encodeBegin(context);
return;
}
HtmlOutputText output = (HtmlOutputText)findComponent("out");
if(null == output){
getLogger().info("can't find output component instance");
output.setValue("null");
super.encodeBegin(context);
return;
}
getLogger().info("input value is:"+inputValue);
output.setValue(inputValue);
super.encodeBegin(context);
}
}

所有代码就这么多,当用户在页面上输入数据后,点击按钮,将会显示刚才输入的数据。具体例子我马上上传到我的空间里。

未完,待续

$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});

原文链接:
http://blog.csdn.net/sheismylife/article/details/6121124
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: