首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Myfaces+Richfaces的自定义组件有关问题

2012-11-09 
Myfaces+Richfaces的自定义组件问题。问题描述: ??? 要写一个继承rich:dataTable的组件。 环境:tomcat 6.0

Myfaces+Richfaces的自定义组件问题。

问题描述:

??? 要写一个继承<rich:dataTable>的组件。

环境:

tomcat 6.0.14myfaces1.1.5richfaces 3.1.2

相应的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-platform-base.xml,classpath:spring-platform-service.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><description>Platform Application</description><display-name>Platform</display-name><context-param><param-name>javax.faces.DEFAULT_SUFFIX</param-name><param-value>.xhtml</param-value></context-param><context-param><param-name>facelets.REFRESH_PERIOD</param-name><param-value>2</param-value></context-param><context-param><param-name>facelets.DEVELOPMENT</param-name><param-value>true</param-value></context-param><context-param><param-name>javax.faces.STATE_SAVING_METHOD</param-name><param-value>server</param-value></context-param><context-param><param-name>com.sun.faces.validateXml</param-name><param-value>true</param-value></context-param><context-param><param-name>com.sun.faces.verifyObjects</param-name><param-value>true</param-value></context-param><context-param><param-name>org.ajax4jsf.VIEW_HANDLERS</param-name><param-value>com.sun.facelets.FaceletViewHandler</param-value></context-param><context-param><param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name><param-value>false</param-value></context-param><filter><display-name>Ajax4jsf Filter</display-name><filter-name>ajax4jsf</filter-name><filter-class>org.ajax4jsf.Filter</filter-class></filter><filter-mapping><filter-name>ajax4jsf</filter-name><servlet-name>Faces Servlet</servlet-name><dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher><dispatcher>INCLUDE</dispatcher><dispatcher>ERROR</dispatcher></filter-mapping><servlet><servlet-name>Faces Servlet</servlet-name><servlet-class>javax.faces.webapp.FacesServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.jsf</url-pattern></servlet-mapping><login-config><auth-method>BASIC</auth-method></login-config><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>index.html</welcome-file></welcome-file-list><!-- 标签配置开始 --><!-- 标签配置结束 --></web-app>

?

tag组件源文件:PFHtmlTableGridTag.java

import javax.faces.component.UIComponent;import javax.faces.webapp.UIComponentTag;public class PFHtmlTableGridTag extends UIComponentTag {public PFHtmlTableGridTag() {System.out.println("hello world");}@Overridepublic String getComponentType() {return "PFHtmlTableGrid";}@Overridepublic String getRendererType() {return null;}@Overrideprotected void setProperties(UIComponent component) {super.setProperties(component);}@Overridepublic void release() {super.release();}}

?

component组件PFHtmlTableGrid.java

import java.io.IOException;import java.util.Date;import javax.faces.component.html.HtmlDataTable;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;public class PFHtmlTableGrid extends HtmlDataTable {@Overridepublic void encodeBegin(FacesContext context) throws IOException {ResponseWriter writer = context.getResponseWriter();writer.startElement("div", this);writer.writeAttribute("style", "color:red", null);writer.writeText("Hello World! today is:"+new Date(), null);writer.endElement("div");}@Overridepublic void encodeEnd(FacesContext context) throws IOException {}@Overridepublic void decode(FacesContext context) {super.decode(context);}@Overridepublic String getFamily() {return null;}}

?

faces-config.xml相应配置:

<component><component-type>PFHtmlTableGrid</component-type><component-class>com.dongyun.platform.component.jsf.tablegrid.PFHtmlTableGrid</component-class></component>

?

标签库文件(存放在WEB/INF目录下):

?

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib><!-- ============== Tag Library Description Elements ============= --><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><short-name>tags</short-name><uri>/WEB-INF/tags.tld</uri><display-name>bookstore</display-name><description>This tag library contains tags used by the bookstoreapplication.</description><!-- ===================== tags for image map component ============================ --><tag><name>tableGrid</name><tag-class>com.dongyun.platform.component.jsf.tablegrid.PFHtmlTableGridTag</tag-class><description>Description of a single hotspot in a client side image map.This tag MUST be nested inside a <map> tag. To specifythe hotspot characteristics, you must specify EITHER a valueOR the alt, coords, and shape attributes.</description></tag></taglib>
?

?

引用该自定义标签的页面:

<?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:rich="http://richfaces.org/rich"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core"xmlns:a4j="http://richfaces.org/a4j" xmlns:cd="/WEB-INF/tags.tld"xml:lang="en" lang="en"><head><title>日志管理界面</title><meta http-equiv="keywords" content="enter,your,keywords,here" /><meta http-equiv="description"content="A short description of this page." /><meta http-equiv="content-type" content="text/html; charset=UTF-8" /></head><body><cd:tableGrid /></body></html>

?

tomcat正常启动没有问题。但是当用firefox打开这个Log.jsf页面时,什么东西都没显示出来。

查看其原代码如下:

<?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:cd="/WEB-INF/tags.tld" xml:lang="en" lang="en"><head><title>日志管理界面</title><meta http-equiv="keywords" content="enter,your,keywords,here" /><meta http-equiv="description" content="A short description of this page." /><meta http-equiv="content-type" content="text/html; charset=UTF-8" /></head><body><cd:tableGrid></cd:tableGrid></body></html>

?

是不是xhtml什么地方没配对,为什么

<cd:tableGrid></cd:tableGrid>

还显示在页面上呢?

大家帮我看看。谢谢了!

?

?

?

?

??

1 楼 chxkyy 2008-01-08   问题解决,拿出来与大家分享。
xhtml用的是JSF facelet的组件。
所以自定义的不是tld标签,即不用tags.tld
而是要创建facelet的标签库。
默认的扩展名:taglib.xml
这里是tags.taglib.xml(一般放在/WEB-INF/classes/META-INF下)
定义的方法与tags.tld差不多。

热点排行