jsf 创建自定义组件

写个自定义按纽的组件,给大家可以参考一下:
 第一步:
package cn.jsf.fgliu

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;

public class NewButtonTag extends UIComponentTag {
    
    private String name;
    private String style;
    private String value;
    
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getComponentType() {
        // TODO 自动生成方法存根
        return "NewButton";
    }

    public String getRendererType() {
        // TODO 自动生成方法存根
        return null;
    }
    protected void setProperties(UIComponent component){
        super.setProperties(component);
        NewButton button = (NewButton) component;
        if (name != null) {
            button.setName(name);
        }
        if(style!=null){
            button.setStyle(style);
        }
        if(value!=null)
            button.setMyValue(value);
        
    }

}

第二步:
package cn.jsf.fgliu

import 编程.io.IOException;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class NewButton extends UIOutput {
   
    private String name;
    private String style;
    private String myValue;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    protected String getComponentType() {
        return "NewButton";
    }
   
    public void encodeEnd(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        writer.write("<input type='button' name='"+ this.getName()+"' value = '"+ this.getMyValue()+"' style='"+this.getStyle()+"'>");
    }

    public String getMyValue() {
        return myValue;
    }

    public void setMyValue(String myValue) {
        this.myValue = myValue;
    }

}


第三步:
在WEB-INF/face-config.xml文件中加入该组件
 <component>
    <component-type>NewButton</component-type>
    <component-class>cn.jsf.fgliu.NewButton</component-class>
  </component>
第四步:
写tld文件
<taglib>
 <tag>
      <name>newButton</name>
      <tag-class>cn.jsf.fgliu.NewButtonTag</tag-class>
      <body-content>JSP</body-content>
      <description>my first button</description>
      <attribute>
         <name>id</name>
         <required>false</required>
         <rtexprvalue>false</rtexprvalue>
      </attribute>
      <attribute>
          <name>name</name>
          <required>false</required>
          <rtexprvalue>false</rtexprvalue>
      </attribute>
      <attribute>
          <name>style</name>
          <required>false</required>
          <rtexprvalue>false</rtexprvalue>
      </attribute>
      <attribute>
          <name>value</name>
          <required>false</required>
          <rtexprvalue>false</rtexprvalue>
      </attribute>
   </tag>
</taglib>

第五步:

在web.xml中加入
<taglib>
      <taglib-uri>http://www.cn.jsf.fgliu/button</taglib-uri>
      <taglib-location>/WEB-INF/button-tag.tld</taglib-location>
  </taglib>

第六步:
jsp页面中使用:
<%@ taglib uri="http://www.cn.jsf.fgliu/button" prefix="fgliu"%>

<fgliu:newButton  name="OK" value="ok"
        style="position:absolute;top:100px;left:61px;height:21px;width:80px;" />
这样就可以了,更多的内容请大家再多多交流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值