`
zben000
  • 浏览: 41916 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

java实现自定义标签

阅读更多

1、新建一个标签的基类,主要是完成一些基本属性的赋值:
package cn.util.tag;

import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author wtu
 * 2008-9-8
 */
public class BaseTag extends TagSupport {
    private String name="";
    private String id="";
    private String onchange="";
    private String onclick="";
    private String css="";
    public String getCss() {
        return css;
    }
    public void setCss(String css) {
        this.css = css;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getOnchange() {
        return onchange;
    }
    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }
    public String getOnclick() {
        return onclick;
    }
    public void setOnclick(String onclick) {
        this.onclick = onclick;
    }
   
    public String init(){
        StringBuffer outContent = new StringBuffer();
        if (this.getId() != null && !"".equals(this.getId())) {
            outContent.append(" id='");
            outContent.append(this.getId());
            outContent.append("'");
        }
        if (this.getName() != null && !"".equals(this.getName())) {
            outContent.append(" name='");
            outContent.append(this.getName());
            outContent.append("'");
        }
        if (this.getOnchange() != null && !"".equals(this.getOnchange())) {
            outContent.append(" onchange='");
            outContent.append(this.getOnchange());
            outContent.append("'");
        }
        if (this.getOnclick() != null && !"".equals(this.getOnclick())) {
            outContent.append(" onclick='");
            outContent.append(this.getOnclick());
            outContent.append("'");
        }
        if (this.getCss() != null && !"".equals(this.getCss())) {
            outContent.append(" class='");
            outContent.append(this.getCss());
            outContent.append("'");
        }
         return outContent.toString();
    }

}

2、一个下拉选择框的标签类,这里主要是完成数据字典的装载:
package cn.util.tag;

import java.util.*;

import javax.servlet.jsp.*;

/**
 * @author wtu
 * 2008-9-8
 */
public class SelectTag extends BaseTag {
    private String def="";
   
    private String dictid="";
   
    public String getDictid() {
        return dictid;
    }

    public void setDictid(String dictid) {
        this.dictid = dictid;
    }

    public String getDef() {
        return def;
    }

    public void setDef(String def) {
        this.def = def;
    }

    public int doStartTag() {
        return (SKIP_BODY);
    }

    public int doEndTag() throws JspException {
        Map dict=(Map)this.pageContext.getServletContext().getAttribute("dict");//从容器内存中读取字典信息
        StringBuffer outContent = new StringBuffer();
        JspWriter out = pageContext.getOut();
        outContent.append("<select ");
        outContent.append(this.init());
        outContent.append(">\r\n");
        outContent.append("<option value=''");
        if (this.getDef() == null)
            outContent.append(" selected");
        outContent.append(">-请选择-</option>\r\n");
        if (dict == null)
            return SKIP_BODY;
        List dictlist=(List) dict.get(this.getDictid());
      
        if(dictlist==null)
            return SKIP_BODY;
       
        for(int i=0;i<dictlist.size;i++) {
            DictInfoBean bean=(DictInfoBean) dictlist.get(i);
            outContent.append("<option value='");
            outContent.append(bean.getValue());
            outContent.append("'");
            if (this.getDef() != null && !"".equals(this.getDef()) &&
                this.getDef().equals(bean.getValue()))
                outContent.append(" selected");
            outContent.append(">");
            outContent.append(getName());
            outContent.append("</option>\r\n");
        }
        outContent.append("</select>\r\n");
        try {
            out.println(outContent);
        } catch (java.io.IOException ie) {
            ie.printStackTrace();
            throw new JspException(ie);
        }
        return SKIP_BODY;
    }
}

3、标签文件(tags.tld)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
   <tlibversion>1.0</tlibversion>
   <jspversion>1.2</jspversion>
   <shortname>dict</shortname>
   <uri>/sz</uri>
   <tag>
     <name>select</name>
     <tagclass>cn.util.tag.SelectTag</tagclass>
     <info>get select value</info>
         <attribute>
           <name>id</name>
           <required>fasle</required>
           <rtexprvalue>true</rtexprvalue>
          </attribute>
        <attribute>
        <name>def</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>css</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>name</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>dictid</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>onchange</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
            <attribute>
        <name>onclick</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
   </tag>
</taglib>

4、web.xml文件配置,添加如下代码:
        <taglib>
            <taglib-uri>/sz</taglib-uri>
            <taglib-location>/WEB-INF/tags.tld</taglib-location>
        </taglib>

5、JSP页面使用:
<%@ taglib prefix="sz" uri="/sz"%>
<sz:label dictid="sex" val="1"></sz:label>

本日志只讲述具体的实现方法,数据字典等代码和数据库结构没给出,请自行完成。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics