自定义标签

1、前段时间由于系统需要使用到数据字典,简而言之就是用下拉将字典的内容显示出来。我也是参考了一下别人的实例结合自己的业务需求,才将此功能实现。在此将大楷实现步奏记录下来,以方便以后翻阅。

2、在我们项目中使用了2张表来实现此功能。

a、CSMS_T_baseInfo(字典名称)

b、CSMS_T_baseItem(主要用户维护字典的键值)

3、创建一个自定义标签实现类

package com.xgd.web.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import com.xgd.CSMS.model.CsmsTBaseitem;
import com.xgd.CSMS.service.ICsmsTBaseitemService;
import com.xgd.utils.SpringBeanUtil;
import com.xgd.utils.StringUtil;
/**
 * TagSupport与BodyTagSupport的区别:
 * 主要看标签处理类是否要读取标签体的内容和改变标签体返回的内容,如果不需要就用TagSupport,否则就用BodyTagSupport
 * 用TagSupport实现的标签,都可以用BodyTagSupport来实现,因为BodyTagSupport继承了TagSupport
 */
@SuppressWarnings("serial")
public class SelectTag extends BodyTagSupport {
    private String keyword = "";
    private String baseinfosqeid = "";
    private boolean defaultValue;
    private String value;
    private String name;
    private String id;
    private String cssClass;
    private String disabled;
    private String readonly;
    private String styleClass;
    private String multiple;
    private String onChange;
    private String dataType;
  
   

    @Override
    public int doEndTag() throws JspException{
       
        @SuppressWarnings("unused")
        CsmsTBaseitem csmsTBaseitem = new CsmsTBaseitem();
        ICsmsTBaseitemService csmsTBaseitemService = (ICsmsTBaseitemService) SpringBeanUtil.getBean("csmsTBaseitemService");
        List<CsmsTBaseitem> baseitemList = csmsTBaseitemService.findByProperty(CsmsTBaseitem.class,"baseinfosqeid",baseinfosqeid);
        JspWriter out = pageContext.getOut();
        StringBuffer sb = new StringBuffer();
        sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"'");
        if(StringUtil.isNotNull(this.getCssClass())){
            sb.append("class=\"" + this.getCssClass() + "\" ");
        }
        if(StringUtil.isNotNull(this.getStyleClass())){
            sb.append("style=\"" + this.getStyleClass() + "\" ");
        }
        if(StringUtil.isNotNull(this.getMultiple())){
            sb.append("multiple=\"" + this.getMultiple() + "\" ");
        }
        if(StringUtil.isNotNull(this.getOnChange())){
            sb.append("onchange=\"" + this.getOnChange() + "\" ");
        }
        if(StringUtil.isNotNull(this.getDisabled())){
            sb.append("disabled=\"" + this.getDisabled() + "\" ");
        }
        if(StringUtil.isNotNull(this.getReadonly())){
            sb.append("readonly=\"" + this.getReadonly() + "\" ");
        }
        sb.append(">");
        if(this.isDefaultValue()){ 
            sb.append("<option value=''>--请选择--</option>"); 
        }
        for(CsmsTBaseitem ctb:baseitemList){
            if(ctb.getBaseitemvalue().equals(this.getValue())){
                sb.append("<option value='"+ctb.getBaseitemvalue()+"' selected='selected'>");
            }else{
                sb.append("<option value='"+ctb.getBaseitemvalue()+"'>");
            }
            sb.append(ctb.getBaseitemname()+"</option>");
        }
        sb.append("</select>");
        try {
            out.write(sb.toString());
        } catch (IOException e) {
            throw new JspException(e);
        }
        return TagSupport.EVAL_PAGE;
    }
   
   
    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public String getMultiple() {
        return multiple;
    }

    public void setMultiple(String multiple) {
        this.multiple = multiple;
    }

    public String getOnChange() {
        return onChange;
    }

    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

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


   
    public String getKeyword() {
        return keyword;
    }


    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }


    public boolean isDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(boolean defaultValue) {
        this.defaultValue = defaultValue;
    }


    public String getBaseinfosqeid() {
        return baseinfosqeid;
    }


    public void setBaseinfosqeid(String baseinfosqeid) {
        this.baseinfosqeid = baseinfosqeid;
    }


    public String getDisabled() {
        return disabled;
    }


    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }


    public String getReadonly() {
        return readonly;
    }


    public void setReadonly(String readonly) {
        this.readonly = readonly;
    }

}

4、在web-html.tld中定义自己的标签(此处我定义了一个name为select 的标签,以及此标签的id、css、事件等属性)

<?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>  <!-- 标签库要求的JSP规范版本 -->
    <short-name>xgd</short-name>   <!-- JSP页面编写工具可以用来创建助记名的可选名字 -->
   <tag>
        <description>Renders an xgd 'select' element. Supports databinding to the selected option.</description>
        <name>select</name>
        <tag-class>com.xgd.web.taglib.SelectTag</tag-class>
        <body-content>JSP</body-content>
        <attribute> 
               <name>defaultValue</name> 
            <required>true</required> 
            <rtexprvalue>true</rtexprvalue> 
        </attribute> 
        <attribute> 
                <name>value</name> 
                <required>false</required> 
                <rtexprvalue>true</rtexprvalue> 
        </attribute>
        <attribute>
            <name>baseinfosqeid</name>
            <required>true</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>id</name>
            <required>true</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssClass</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>styleClass</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>multiple</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>onChange</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>disabled</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>readonly</name>
            <required>false</required> 
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
   
</taglib>

 

5、在web.xml中加载自定义标签

  <!-- 加载自定义标签 -->
    <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/xgdSelectTag.tld</taglib-uri>
            <taglib-location>/WEB-INF/xgdSelectTag.tld</taglib-location>
        </taglib>
    </jsp-config>

6、JSP中的使用(baseinfosqeid="SQ0004" 数据库中需要有此数据)

<xgd:select defaultValue="true" name="csmsTSecretkey.skAlgorithm" id="skAm" baseinfosqeid="SQ0004" cssClass="combox"></xgd:select>

 

 

 

你可能感兴趣的:(自定义标签)