浏览 779 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-01-07
问题描述: 要写一个继承<rich:dataTable>的组件。 环境:
相应的配置文件如下: <?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> 还显示在页面上呢? 大家帮我看看。谢谢了!
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-01-08
问题解决,拿出来与大家分享。
xhtml用的是JSF facelet的组件。 所以自定义的不是tld标签,即不用tags.tld 而是要创建facelet的标签库。 默认的扩展名:taglib.xml 这里是tags.taglib.xml(一般放在/WEB-INF/classes/META-INF下) 定义的方法与tags.tld差不多。 |
|
| 返回顶楼 | |


