2008-01-07
Myfaces+Richfaces的自定义组件问题。
问题描述:
要写一个继承<rich:dataTable>的组件。
环境:
- tomcat 6.0.14
- myfaces1.1.5
- richfaces 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");
}
@Override
public String getComponentType() {
return "PFHtmlTableGrid";
}
@Override
public String getRendererType() {
return null;
}
@Override
protected void setProperties(UIComponent component) {
super.setProperties(component);
}
@Override
public 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 {
@Override
public 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");
}
@Override
public void encodeEnd(FacesContext context) throws IOException {
}
@Override
public void decode(FacesContext context) {
super.decode(context);
}
@Override
public 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 bookstore
application.
</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 specify
the hotspot characteristics, you must specify EITHER a value
OR 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>
还显示在页面上呢?
大家帮我看看。谢谢了!
评论
chxkyy
2008-01-08
问题解决,拿出来与大家分享。
xhtml用的是JSF facelet的组件。
所以自定义的不是tld标签,即不用tags.tld
而是要创建facelet的标签库。
默认的扩展名:taglib.xml
这里是tags.taglib.xml(一般放在/WEB-INF/classes/META-INF下)
定义的方法与tags.tld差不多。
xhtml用的是JSF facelet的组件。
所以自定义的不是tld标签,即不用tags.tld
而是要创建facelet的标签库。
默认的扩展名:taglib.xml
这里是tags.taglib.xml(一般放在/WEB-INF/classes/META-INF下)
定义的方法与tags.tld差不多。
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 17966 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
ie7
共 2 张
共 2 张
最近加入圈子
链接
最新评论
-
Ajax4jsf 和 <h:message> ...
给<h:message> 加个ID标识 <a4j:commandBu ...
-- by zuzuzuu -
response.sendRedirect(lo ...
说的很详细,谢了啊
-- by ungshow -
Ajax4jsf 和 <h:message> ...
能把你的页面的代码贴上来看一下吗?包括<h:message>
-- by chxkyy -
Ajax4jsf 和 <h:message> ...
<a4j:commandButton value="Add" style ...
-- by rainingrose -
Firefox 3没办法用QZone?
stevenwang 写道ff3对中文支持的不好。访问个别网站乱码。查看->字符 ...
-- by qubic






评论排行榜