JSF自定义组件之三 JSF实现-Tag

46 篇文章 0 订阅
29 篇文章 0 订阅

    上篇已介绍了完整的HTML实现,从这篇开始,将前面介绍的功能逐步地用JSF组件来实现。

    分析前面的HTML页面,我们简单为该标签设置四个属性,分别为:valueList -- 下拉列表, value -- 值, image -- 下拉按钮图片, styleClass -- 用来指定文本框的格式。

    因此,继承类javax.faces.webapp.UIComponentELTag,生成DropdownListTag类,代码如下:

  1. package net.moon.jsf.customer.tag;
  2. import javax.el.ValueExpression;
  3. import javax.faces.component.UIComponent;
  4. import javax.faces.webapp.UIComponentELTag;
  5. import net.moon.jsf.customer.component.HtmlDropdownList;
  6. public class DropdownListTag extends UIComponentELTag {
  7.     private static final String RENDER_TYPE = "net.moon.DropdownList";
  8.     private static final String COMPONENT_TYPE = "DropdownList";
  9.     private ValueExpression value;
  10.     private ValueExpression valueList;
  11.     private ValueExpression styleClass;
  12.     private ValueExpression image;
  13.     
  14.     public void setValue(ValueExpression value) {
  15.         this.value = value;
  16.     }
  17.     public void setValueList(ValueExpression valueList) {
  18.         this.valueList = valueList;
  19.     }
  20.     public void setStyleClass(ValueExpression styleClass) {
  21.         this.styleClass = styleClass;
  22.     }
  23.     public void setImage(ValueExpression image) {
  24.         this.image = image;
  25.     }
  26.     @Override
  27.     public String getComponentType() {
  28.         // TODO Auto-generated method stub
  29.         return COMPONENT_TYPE;
  30.     }
  31.     @Override
  32.     public String getRendererType() {
  33.         // TODO Auto-generated method stub
  34.         return RENDER_TYPE;
  35.     }
  36.     @Override
  37.     public void release() {
  38.         // TODO Auto-generated method stub
  39.         super.release();
  40.         value = null;
  41.         valueList = null;
  42.         styleClass = null;
  43.         image = null;
  44.     }
  45.     @Override
  46.     protected void setProperties(UIComponent component) {
  47.         // TODO Auto-generated method stub
  48.         super.setProperties(component);
  49.         
  50.         HtmlDropdownList dropdown = (HtmlDropdownList) component;
  51.         if (valueList != null) {
  52.             if (!valueList.isLiteralText()) {
  53.                 dropdown.setValueExpression("valueList", valueList);
  54.             } else {
  55.                 dropdown.setValueList(valueList.getExpressionString());
  56.             }
  57.         }
  58.         if (value != null) {
  59.             if (!value.isLiteralText()) {
  60.                 dropdown.setValueExpression("value", value);
  61.             } else {
  62.                 dropdown.getAttributes().put("value", value);
  63.             }
  64.         }
  65.         if (styleClass != null) {
  66.             if (!styleClass.isLiteralText()) {
  67.                 dropdown.setValueExpression("styleClass", styleClass);
  68.             } else {
  69.                 dropdown.getAttributes().put("class", styleClass);
  70.             }
  71.         }
  72.         if (image != null) {
  73.             if (!image.isLiteralText()) {
  74.                 dropdown.setValueExpression("image", image);
  75.             } else {
  76.                 dropdown.setImage(image.getExpressionString());
  77.             }
  78.         }
  79.     }
  80.     public DropdownListTag() {
  81.         super();
  82.         // TODO Auto-generated constructor stub
  83.     }
  84.     
  85. }

    另外,编写如下tld文件,描述标签:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <taglib
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  4.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     version="2.1">
  6.     <description>This is a custmoer component, include server site
  7.         dropdown list, to make the large size drop down list can be shown not
  8.         at the initial time, but when click the down button </description>
  9.     <tlib-version>1.0</tlib-version>
  10.     <short-name>moon</short-name>
  11.     <uri>http://www.moonsnow.net/jsf</uri>
  12.     <tag>
  13.         <description>this is the drop down list tag</description>
  14.         <name>dropdownList</name>
  15.         <tag-class>net.moon.jsf.customer.tag.DropdownListTag</tag-class>
  16.         <body-content>JSP</body-content>
  17.         <attribute>
  18.             <name>value</name>
  19.             <required>false</required>
  20.             <rtexprvalue>true</rtexprvalue>
  21.             <deferred-value>
  22.                 <type>java.lang.String</type>
  23.             </deferred-value>
  24.         </attribute>
  25.         <attribute>
  26.             <description>this is the list of string which you want to show
  27.                 for this dropdown list</description>
  28.             <name>valueList</name>
  29.             <required>true</required>
  30.             <deferred-value>
  31.                 <type>java.util.List</type>
  32.             </deferred-value>
  33.         </attribute>
  34.         <attribute>
  35.             <description>the style class of the text box</description>
  36.             <name>styleClass</name>
  37.             <rtexprvalue>false</rtexprvalue>
  38.             <type>java.lang.String</type>
  39.         </attribute>
  40.         <attribute>
  41.             <description>the image for the drop down button</description>
  42.             <name>image</name>
  43.             <deferred-value>
  44.                 <type>java.lang.String</type>
  45.             </deferred-value>
  46.         </attribute>
  47.     </tag>
  48. </taglib>

 

    其中,Tag类中的ComponentType和RendererType两个属性较为重要,在后面篇章中将综合介绍其具体配置方式。

 

    下一篇将介绍Component类的而开发。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值