1.Tag类
package ticker;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
public class TickerTag extends UIComponentTag {
public void release() {
// the super class method should be called
super.release();
}
protected void setProperties(UIComponent component) {
// the super class method should be called
super.setProperties(component);
}
public String getComponentType() {
return "ticker";
}
public String getRendererType() {
// null means the component renders itself
return "ticker.renderer";
}
}
2.UI类
package ticker;
import java.io.IOException;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class UITicker extends UIOutput {
public UITicker(){
setRendererType("ticker.renderer");
}
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.write(" <link type='text/css' rel='StyleSheet' href='css/datepicker.css'/> \n" +
"<script type='text/javascript' src='js/datepicker.js'></script> \n"+
"<script type='text/javascript'> \n " +
"var d = new Date();\n" +
" var dp = new DatePicker(d,d);\n" +
"document.body.appendChild(dp.create());\n" +
"</script>");
}
public void encodeEnd(FacesContext context) throws IOException {
}
}
3.Tld档
<?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>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>d</short-name>
<uri>http://jsftutorials.com/</uri>
<tag>
<name>ticker</name>
<tag-class>ticker.TickerTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
4.faces-config 配置
<component>
<component-type>ticker</component-type>
<component-class>ticker.UITicker</component-class>
</component>