自定义标签

类dictionaryTag 必须继承TagSupport
public class dictionaryTag extends TagSupport {

private static final long serialVersionUID = 3628217258733508857L;

private String type;
private String flag;

public int doStartTag() throws JspException {
try {
WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext(pageContext.getServletContext());

DictionaryManager dictionaryManager = (DictionaryManager) wac
.getBean("dictionaryManager");

Dictionary dictionary = new Dictionary();
dictionary.setDicTypeNo(type);
List list = dictionaryManager.queryByType(dictionary);
String str = "";

if (list.get(0) != null) {
str = "[";
//如果flag.equals("0")为一般情况
if(flag != null && flag.equals("0")){
for (int i = 0; i < list.size(); i++) {
Dictionary dictionaryObj = (Dictionary) list
.get(i);
str += "['" + dictionaryObj.getDicId() + "','"
+ dictionaryObj.getDicName() + "','"
+ dictionaryObj.getDicTypeNo() + "','"
+ dictionaryObj.getDescription() + "','"
+ String.valueOf(dictionaryObj.getSortnum())
+ "']";
if (i != (list.size() - 1))
str = str + ",";
}
}else{//如果flag.equals("1")做特殊处理
for (int i = 0; i < list.size(); i++) {
Dictionary dictionaryObj = (Dictionary) list
.get(i);
str += "['" + dictionaryObj.getDescription() + "','"
+ dictionaryObj.getDicName() + "','"
+ dictionaryObj.getDicTypeNo() + "','"
+ dictionaryObj.getDicId() + "','"
+ String.valueOf(dictionaryObj.getSortnum())
+ "']";
if (i != (list.size() - 1))
str = str + ",";
}
}
str += "]";
} else {
str = "[]";
}
System.out.println(str);
pageContext.getOut().print(str);
} catch (java.io.IOException ex) {
System.out.println(ex);
throw new JspException(ex.getMessage());
}
/* 禁止处理主体部分 */
return SKIP_BODY;
}

public int doEndTag() throws JspException {
/* 处理页面其它部分 */
return EVAL_PAGE;
}

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


public void setFlag(String flag) {
this.flag = flag;
}

}
返回的str内容:[['501','加油站油库','500','加油站油库','501'],['530','烟花爆竹零售网点','500','烟花爆竹零售网点','530'],['550','工业企业','500','工业企业','550'],['570','作业现场职业病防治','500','作业现场职业病防治','570'],['600','劳动用工','500','劳动用工','600'],['620','防洪安全','500','防洪安全','620'],['630','开凿机井','500','开凿机井','630'],['640','公园、林地火险隐患','500','公园、林地火险隐患','640'],['660','林业有害生物防治','500','林业有害生物防治','660'],['680','危化生产运输活动的环境安全','500','危化生产运输活动的环境安全','680'],['700','建筑工地','500','建筑工地','700'],['760','临时宣传品(条幅展板、气拱门升空气球)','500','临时宣传品(条幅展板、气拱门升空气球)','760'],['780','户外广告','500','户外广告','780'],['800','牌匾标识','500','牌匾标识','800'],['820','占用、挖掘市政道路','500','占用、挖掘市政道路','820'],['840','燃气安全','500','燃气安全','840'],['850','电力安全','500','电力安全','850'],['870','餐饮企业','500','餐饮企业','870'],['900','商业零售','500','商业零售','900'],['930','体育运动项目经营单位','500','体育运动项目经营单位','930'],['950','旅游景区','500','旅游景区','950'],['980','星级饭店','500','星级饭店','980'],['1000','歌舞厅','500','歌舞厅','1000'],['1020','网吧','500','网吧','1020'],['1040','演出场所','500','演出场所','1040'],['1060','电影院','500','电影院','1060'],['1080','人防工程','500','人防工程','1080'],['1110','通信警报','500','通信警报','1110'],['1140','特种设备生产使用单位','500','特种设备生产使用单位','1140'],['1170','有形市场','500','有形市场','1170'],['1200','学校','500','学校','1200'],['1270','燃气','500','燃气','1270'],['1280','清洁燃料车辆加气站','500','清洁燃料车辆加气站','1280'],['1290','消防安全','500','消防安全','1290'],['1310','煤气中毒','500','煤气中毒','1310'],['1320','交通安全','500','交通安全','1320'],['1340','省际客运安全','500','省际客运安全','1340'],['1360','城市轨道交通安全','500','城市轨道交通安全','1360'],['1370','道路货物运输安全','500','道路货物运输安全','1370'],['1390','机动车维修','500','机动车维修','1390'],['1400','水运安全','500','水运安全','1400'],['1410','汽车租赁','500','汽车租赁','1410'],['1420','停车场','500','停车场','1420'],['1440','物业公司','500','物业公司','1440'],['1470','仓储物流公司','500','仓储物流公司','1470'],['1490','其他企业','500','其他企业','1490'],['1500','其他危险化学品经营单位','500','其他危险化学品经营单位','1500'],['1600','大型活动','500','大型活动','1600'],['1610','剧毒物品','500','剧毒物品','1610']]


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>dictionary</shortname>
<uri>http://www.itsv.com/platform/dictionary</uri>
  <tag>
    <name>dictionaryTag</name>
    <tagclass>com.itsv.cyaj.tag.dictionaryTag</tagclass>
    <bodycontent>empty</bodycontent>
    <attribute>
      <name>type</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>flag</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>

jsp页面

var dicttroubleType = <dictionary:dictionaryTag type="500" flag="0" />;//获得隐患类型

var obj_troubletype = new itsvDictionary(dicttroubleType);

function itsvDictionary(dataItems){
  //按照顺序编号排序
  /*
  function _sortByNo(a, b) {
      if(Number(a[2]) == Number(b[2])) return 0
      return (Number(a[2]) > Number(b[2])) ? 1 : -1;
  }
  this.items = dataItems.sort(_sortByNo);
  */
  this.items = dataItems;
  this.execute = function(obj){
    obj.length = 0;
    obj.options[obj.length] = new Option("请选择", "");
    for(i = 0; i < this.items.length; i++){
      obj.options[obj.length] = new Option(this.items[i][1], this.items[i][0]);
    }
  }
  this.execute2 = function(obj){//没有请选择
    obj.length = 0;
    for(i = 0; i < this.items.length; i++){

      obj.options[obj.length] = new Option(this.items[i][1], this.items[i][0]);
    }
  }
  this.setDefault = function(obj, defaultVal){
    for(i = 0; i < this.items.length; i++){
      if(Number(this.items[i][0]) == Number(defaultVal)){
        obj.options[i + 1].selected = true;
        break;
      }
    }
  }
  this.setDefault2 = function(obj, defaultVal){
    for(i = 0; i < this.items.length; i++){
      if(Number(this.items[i][0]) == Number(defaultVal)){
        obj.options[i].selected = true;
        break;
      }
    }
  }
  this.getName = function(code){
    for(i = 0; i < this.items.length; i++){
      if(Number(this.items[i][0]) == Number(code)){
        return this.items[i][1];
      }
    }
  }
}

你可能感兴趣的:(物流,活动,体育,交通,旅游)