记录下来做过的点点滴滴,以防忘掉哦!
一、建立tld文件
htTag.tld中的内容如下(选择一个标签):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>ht</shortname>
<!-- 位图展示 -->
<tag>
<name>bitMap</name>
<tagclass>com.huateng.common.struts.taglib.BitMapTag</tagclass>
<info>对字符实施长度限制</info>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bitName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bitValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>lineSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssClass</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
二、实现com.huateng.common.struts.taglib.BitMapTag
package com.huateng.common.struts.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* desc:位图展示
*
* @author
*
* @version SVN $Revision: 1.1 $ $Date: Jul 2, 2013 10:15:14 AM $
*
* @example:<ht:bitMap name="tidInfo.termCardAuthBmp" bitName="${bitNames}" bitValue="${tidInfo.termCardAuthBmp}" lineSize="3"></ht:bitMap>
*/
public class BitMapTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = -5538172184142608614L;
/**
* 名称
*/
private String name;
/**
* 位图中文名,多个用","分割
*/
private String bitName;
/**
* 位图实际值,用1\0\F等表示的字符串
*/
private String bitValue;
/**
* 每行展示的位图(checkbox)个数
*/
private String lineSize;
/**
* css
*/
private String cssClass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBitName() {
return bitName;
}
public void setBitName(String bitName) {
this.bitName = bitName;
}
public String getBitValue() {
return bitValue;
}
public void setBitValue(String bitValue) {
this.bitValue = bitValue;
}
public String getLineSize() {
return lineSize;
}
public void setLineSize(String lineSize) {
this.lineSize = lineSize;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().write(build());
} catch (IOException io) {
throw new JspException(io);
}
return super.doStartTag();
}
@Override
public void release() {
super.release();
}
/**
* @return
*/
public String build(){
String[] bitNames = null;
if(bitName != null && !"".equals(bitName)){
bitNames = bitName.split(",");
}else{
return null;
}
char[] values = null;
if(bitValue != null && !"".equals(bitValue)){
values = bitValue.toCharArray();
}
StringBuffer sb = new StringBuffer();
sb.append("<div>");
sb.append("<table width='100%'>");
sb.append("<tr>");
for(int i = 0; i < bitNames.length; i++){
if(i != 0 && i % Integer.parseInt(lineSize) == 0){
sb.append("</tr><tr>");
}
sb.append("<td>");
String checked = "";
if(values!=null && values.length>i && "1".equals(String.valueOf(values[i]))){
checked = "checked='true'";
}
sb.append("<input type='checkBox' class='"+cssClass+"' name='"+name+"' value='"+i+"' "+checked+">"+bitNames[i]+"</input>");
sb.append("</td>");
}
sb.append("</tr>");
sb.append("</table>");
sb.append("</div>");
return sb.toString();
}
}
三、引入
在jsp头引入如下:
<%@ taglib uri="/WEB-INF/tld/htTag.tld" prefix="ht" %>
四、可以用了哦
<h6>产品权限位图:</h6>
<ht:bitMap name="tidInfo.termProdBmp" bitName="${prodNames}" bitValue="${tidInfo.termProdBmp}" lineSize="4"></ht:bitMap>