上篇已介绍了完整的HTML实现,从这篇开始,将前面介绍的功能逐步地用JSF组件来实现。
分析前面的HTML页面,我们简单为该标签设置四个属性,分别为:valueList -- 下拉列表, value -- 值, image -- 下拉按钮图片, styleClass -- 用来指定文本框的格式。
因此,继承类javax.faces.webapp.UIComponentELTag,生成DropdownListTag类,代码如下:
- package net.moon.jsf.customer.tag;
- import javax.el.ValueExpression;
- import javax.faces.component.UIComponent;
- import javax.faces.webapp.UIComponentELTag;
- import net.moon.jsf.customer.component.HtmlDropdownList;
- public class DropdownListTag extends UIComponentELTag {
- private static final String RENDER_TYPE = "net.moon.DropdownList";
- private static final String COMPONENT_TYPE = "DropdownList";
- private ValueExpression value;
- private ValueExpression valueList;
- private ValueExpression styleClass;
- private ValueExpression image;
- public void setValue(ValueExpression value) {
- this.value = value;
- }
- public void setValueList(ValueExpression valueList) {
- this.valueList = valueList;
- }
- public void setStyleClass(ValueExpression styleClass) {
- this.styleClass = styleClass;
- }
- public void setImage(ValueExpression image) {
- this.image = image;
- }
- @Override
- public String getComponentType() {
- // TODO Auto-generated method stub
- return COMPONENT_TYPE;
- }
- @Override
- public String getRendererType() {
- // TODO Auto-generated method stub
- return RENDER_TYPE;
- }
- @Override
- public void release() {
- // TODO Auto-generated method stub
- super.release();
- value = null;
- valueList = null;
- styleClass = null;
- image = null;
- }
- @Override
- protected void setProperties(UIComponent component) {
- // TODO Auto-generated method stub
- super.setProperties(component);
- HtmlDropdownList dropdown = (HtmlDropdownList) component;
- if (valueList != null) {
- if (!valueList.isLiteralText()) {
- dropdown.setValueExpression("valueList", valueList);
- } else {
- dropdown.setValueList(valueList.getExpressionString());
- }
- }
- if (value != null) {
- if (!value.isLiteralText()) {
- dropdown.setValueExpression("value", value);
- } else {
- dropdown.getAttributes().put("value", value);
- }
- }
- if (styleClass != null) {
- if (!styleClass.isLiteralText()) {
- dropdown.setValueExpression("styleClass", styleClass);
- } else {
- dropdown.getAttributes().put("class", styleClass);
- }
- }
- if (image != null) {
- if (!image.isLiteralText()) {
- dropdown.setValueExpression("image", image);
- } else {
- dropdown.setImage(image.getExpressionString());
- }
- }
- }
- public DropdownListTag() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
另外,编写如下tld文件,描述标签:
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglib
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- version="2.1">
- <description>This is a custmoer component, include server site
- dropdown list, to make the large size drop down list can be shown not
- at the initial time, but when click the down button </description>
- <tlib-version>1.0</tlib-version>
- <short-name>moon</short-name>
- <uri>http://www.moonsnow.net/jsf</uri>
- <tag>
- <description>this is the drop down list tag</description>
- <name>dropdownList</name>
- <tag-class>net.moon.jsf.customer.tag.DropdownListTag</tag-class>
- <body-content>JSP</body-content>
- <attribute>
- <name>value</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- <deferred-value>
- <type>java.lang.String</type>
- </deferred-value>
- </attribute>
- <attribute>
- <description>this is the list of string which you want to show
- for this dropdown list</description>
- <name>valueList</name>
- <required>true</required>
- <deferred-value>
- <type>java.util.List</type>
- </deferred-value>
- </attribute>
- <attribute>
- <description>the style class of the text box</description>
- <name>styleClass</name>
- <rtexprvalue>false</rtexprvalue>
- <type>java.lang.String</type>
- </attribute>
- <attribute>
- <description>the image for the drop down button</description>
- <name>image</name>
- <deferred-value>
- <type>java.lang.String</type>
- </deferred-value>
- </attribute>
- </tag>
- </taglib>
其中,Tag类中的ComponentType和RendererType两个属性较为重要,在后面篇章中将综合介绍其具体配置方式。
下一篇将介绍Component类的而开发。