html的input 标签自定义属性

时间:2022-11-02 13:49:25

例如自定义属性serial,

    <input id="button1" type="button" value="购买"   onclick="return Button1_onclick(this.serial-1)" serial="1" />

当然也可以通过css的style属性设置样式

<input type="text" id="t1" value="" style="width:100px;height:100px"/>

然后通过js来控制function textControl(){ document.getElementById("t1").style.width="100%"; document.getElementById("t1").style.height="100%";}

 

最后就是自定义标签库TLD,当然就可以自定义属性了;

下面就简单那解释如何自定义标签库TLD(一个标签对应一个java类,还需要在web.xml文件中配置)

下面是自定义标签库TLD的xml格式文件test.tld内容:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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.1</tlib-version>

<!--定义jsp版本库-->
  <jsp-version>1.2</jsp-version>
  <short-name>testOne</short-name>
  <uri>http://www.johnit.com.cn/tags-test</uri>
  <display-name>testOne</display-name>
  <description>user defination tag</description>

  <validator>

<!--校验器类--> 
    <validator-class>
     com.johnit.common.web.util.JstlTLV
    </validator-class>   
    <init-param>
 <param-name>expressionAttributes</param-name>
 <param-value>  
  loadInfo
 </param-value>
 <description>
     user defination tag.
 </description>
     </init-param>
  </validator> 
 
  <tag>

 <!--tag的名字--> 
    <name>loadInfoPopedom</name>
    <tag-class>com.johnit.common.web.util.LoadInfoPopedomTag</tag-class>
    <body-content>empty</body-content>

<!--关于body-content 有三个值可选;empty:标签体必须为空;jsp:标签由其他jsp元素组成比如标签中含有<%=attributeName%>的jsp元素,那么此时body-content的值就是实际attributeName传入的值;tagdependent:有标签解释不带jsp转换--> 
    <description>
        loadInfoPopedom
    </description>
    <attribute><!---这里表示的是这个tag的一个参数-->
            <name>id</name><!--这个参数的名字-->
            <required>false</required><!--是否是必填选项-->
            <rtexprvalue>false</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值--> 
    </attribute>
    <attribute>
        <name>name</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>width</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>height</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>class</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>style</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>validate</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>size</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>options</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
 
  <tag>
    <name>input</name>
     <tag-class>com.johnit.common.web.util.LimitTag</tag-class>
    <body-content>empty</body-content>
    <description>
        user  role  limit
    </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>value</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>width</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>height</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>class</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>style</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>type</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>size</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>roles</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>onclick</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>disabled</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>showType</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  <tag>

         // 这里可以增加其他的标签
     </tag>
</taglib>

 

接着针对每一个标签tag,写一个对应的java类,以上面com.johnit.common.web.util.LimitTag标签为例子,这java是继承BodyTagSupport(也可以继承TagSupport,其中BodyTagSupport是TagSupport的子类,但是如果继承TagSupport 而且要覆盖doEndTag()方法 )

package com.johnit.common.web.util;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
import org.apache.taglibs.standard.tag.el.core.ExpressionUtil;


public class LimitTag extends BodyTagSupport {

  private static final long serialVersionUID = 3337803917881831206L;
  //显示方式:0 无、1 隐藏、2 不可用
  static final String SHOW_NONE="0";
  static final String SHOW_HIDDEN="1";
  static final String SHOW_DISABLED="2";
  public LimitTag() {
    init();
  }

  private void init() {
  }

  public void release() {
    super.release();
    init();
  }

  public int doStartTag() throws JspException {
    //业务方法
    return super.doStartTag();
  }
/**
 * 这里就添加业务方法
 */
 
  protected String id;

  protected String name;

  protected String width;

  protected String height;

  protected String clazz;

  protected String style;
 
  protected String type;

  protected String value;

  protected String size;

  private String onclick;
 
  private String disabled;
 
  private String roles;

  private String showType;
 
  public String getShowType() {
    return showType;
  }

  public void setShowType(String showType) {
    this.showType = showType;
  }

  public String getRoles() {
    return roles;
  }

  public void setRoles(String roles) {
    this.roles = roles;
  }

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

  public void setSize(String size) {
    this.size = size;
  }

  public void setType(String type) {
    this.type = type;
  }

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


  public String getDisabled() {
    return disabled;
  }

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

  public String getOnclick() {
    return onclick;
  }

  public void setOnclick(String onclick) {
    this.onclick = onclick;
  }

  public String getSize() {
    return size;
  }

  public String getType() {
    return type;
  }

  public String getValue() {
    return value;
  }

public String getClazz() {
 return clazz;
}

public void setClazz(String clazz) {
 this.clazz = clazz;
}

public String getHeight() {
 return height;
}

public void setHeight(String height) {
 this.height = height;
}

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 getWidth() {
 return width;
}

public void setWidth(String width) {
 this.width = width;
}

public String getId() {
 return id;
}
/*******************************************************************************************************************
 * 根据validate添加引号 <p> 行为描述: 状态转变: 定义的算法: 操作系统/硬件依赖: 允许的实现变化: 安全约束: </p>
 *
 * @param
 * @return
 * @throws
 * @see
 * @since %I%
 */
protected static String addQuotation(String str) {
    int counter = 0;
    StringBuffer sb = new StringBuffer(str.length());
    sb.append('\'');
    for (int i = 0; i < str.length(); i++) {
        boolean findQuate = false;
        if (str.charAt(i) == '[') {
            counter++;
        } else if (str.charAt(i) == ']') {
            counter--;
        } else if (str.charAt(i) == ',' && counter == 0) {
            findQuate = true;
        }
        if (findQuate) {
            sb.append('\'').append(str.charAt(i)).append('\'');
        } else {
            sb.append(str.charAt(i));
        }
    }
    sb.append('\'');
    return sb.toString();
}

/*******************************************************************************************************************
 * 获取${xxx}的值 <p> 行为描述: 状态转变: 定义的算法: 操作系统/硬件依赖: 允许的实现变化: 安全约束: </p>
 *
 * @param
 * @return
 * @throws
 * @see
 * @since %I%
 */
protected Object getExp(String express) throws JspException {
    if (express == null) {
        return "";
    } else if (!express.startsWith("${")) {
        return express;
    }
    try {
        return ExpressionUtil.evalNotNull("out", "value", express, java.lang.Object.class, this, pageContext);
    } catch (NullAttributeException e) {
        return null;
    } catch (JspException e) {
        throw e;
    }
}

protected String getStrExp(String express) throws JspException {
    return (String) getExp(express);
}

protected static String escape(String text) {
    if (text == null) return "";
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (c == '&') {
            sb.append("&amp;");
            continue;
        }
        if (c == '<') {
            sb.append("&lt;");
            continue;
        }
        if (c == '>') {
            sb.append("&gt;");
            continue;
        }
        if (c == '"') {
            sb.append("&#034;");
            continue;
        }
        if (c == '\'') {
            sb.append("&#039;");
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}
}

 

最后在web.xml中的<web-app>需要添加

 <taglib>
    <taglib-uri>http://www.johnit.com.cn/tags-test</taglib-uri>
    <taglib-location>/WEB-INF/test.tld</taglib-location>
  </taglib>