JSP 自定义标签 的 开发
程序员文章站
2022-04-25 21:53:12
...
为了简单说明,拿系统代码处理为例,根据标识获取名称
定义xxx.tld文件,放在WEB-INF下的tld中
web.xml中配置所写的xxx.tld
写标签类
最后页面使用
定义xxx.tld文件,放在WEB-INF下的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.2</tlibversion> <jspversion>1.1</jspversion> <shortname>cpctld</shortname> <uri>/fulg</uri> <tag> <name>xiTongDaiMaMingCheng</name> <tagclass>com.xxx.xxx.tag.XiTongDaiMaNameTag</tagclass> <bodycontent>empty</bodycontent> <description> <![CDATA[根据系统代码的整形代码或字符代码获取名称,标签中可选择任意一种定义并赋值]]> </description> <attribute> <name>biaoshi</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description> <![CDATA[当前系统代码的父节点标识,必定义]]> </description> </attribute> <attribute> <name>mingCheng</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> <![CDATA[当前系统代码的父节点名称,仅作显示指导使用,非必定义]]> </description> </attribute> <attribute> <name>zhengXingDaiMa</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> <![CDATA[整形代码,非必定义,但与字符代码至少定义一个]]> </description> </attribute> <attribute> <name>ziFuDaiMa</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> <![CDATA[整形代码,非必定义,但与字符代码至少定义一个]]> </description> </attribute> </tag> </taglib>
web.xml中配置所写的xxx.tld
<jsp-config> <taglib> <taglib-uri>/fulg</taglib-uri> <taglib-location>/WEB-INF/tld/xxx.tld</taglib-location> </taglib> </jsp-config>
写标签类
package xxx.xxx.tag; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.WeakHashMap; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * @ClassName: XiTongDaiMaNameTag * @Description: TODO(系统代码标签,根据zhengXingDaiMa或ziFuDaiMa获取整形代码名称,两种情况标签中至少写一个,都写按ziFuDaiMa查询) * @Company neusoft * @author fulg * @date 2013-10-30 */ public class XiTongDaiMaNameTag extends TagSupport{ //父节点标识 private int biaoshi; //父节点名称,非必填项,仅作显示说明使用,代码里显示当前系统代码的名称 private String mingCheng; //整形代码 private int zhengXingDaiMa; //字符代码 private String ziFuDaiMa; //名称缓存 //private WeakHashMap nameMap;//使用map缓存后,系统代码树的修改无法实时反映到map中 @Override public int doStartTag() throws JspException { String xiTongDaiMaMingCheng = ""; //if(nameMap==null||nameMap.isEmpty()){ ContextHolder.setCustomerType(SysConstants.CURRENT_SERVER); //系统代码实例对象 IXiTongDaiMaService xiTongDaiMaService = (IXiTongDaiMaService)AppContextUtils.getBean("xiTongDaiMaService"); //根据父类查询系统代码列表 List<XiTongDaiMa> list = null; //if(mingCheng!=null){//不以名称查询,因为名称可以改变 // list = xiTongDaiMaService.getXiTongDaiMaByFenLeiMingCheng(mingCheng); //}else{ list = xiTongDaiMaService.getXiTongDaiMaByFuJieDianBiaoShi(biaoshi); //}//else end //nameMap = new WeakHashMap(); for (XiTongDaiMa vo:list) { if(ziFuDaiMa!=null){ //nameMap.put(vo.getZiFuDaiMa(), vo.getMingCheng()); if(ziFuDaiMa.equals(vo.getZiFuDaiMa())){ xiTongDaiMaMingCheng = vo.getMingCheng(); break; } }else{ //nameMap.put(vo.getZhengXingDaiMa(), vo.getMingCheng()); if(zhengXingDaiMa==vo.getZhengXingDaiMa()){ xiTongDaiMaMingCheng = vo.getMingCheng(); break; } } } //}//if nameMap==null /*if(ziFuDaiMa!=null&&!ziFuDaiMa.equals("")){ xiTongDaiMaMingCheng = (String)nameMap.get(ziFuDaiMa); }else{ xiTongDaiMaMingCheng = (String)nameMap.get(zhengXingDaiMa); }*/ //此方法里也可以封装jsp页面元素等操作 //例如String str = "<select id='xxx' name='xxx'><option value="+vo.getZhengXingDaiMa()+">"+vo.getMingCheng()+"</option></select>" //注:option需要for循环等操作 //然后把会写到页面就可以了out.write(str); try { JspWriter out = pageContext.getOut(); out.write(xiTongDaiMaMingCheng); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); } return SKIP_BODY; } @Override public int doEndTag() throws JspException { return EVAL_PAGE; } public int getBiaoshi() { return biaoshi; } public void setBiaoshi(int biaoshi) { this.biaoshi = biaoshi; } public String getMingCheng() { return mingCheng; } public void setMingCheng(String mingCheng) { this.mingCheng = mingCheng; } public int getZhengXingDaiMa() { return zhengXingDaiMa; } public void setZhengXingDaiMa(int zhengXingDaiMa) { this.zhengXingDaiMa = zhengXingDaiMa; } public String getZiFuDaiMa() { return ziFuDaiMa; } public void setZiFuDaiMa(String ziFuDaiMa) { this.ziFuDaiMa = ziFuDaiMa; } /*public WeakHashMap getNameMap() { return nameMap; } public void setNameMap(WeakHashMap nameMap) { this.nameMap = nameMap; }*/ }
最后页面使用
<td nowrap="nowrap"> <fulg:xiTongDaiMaMingCheng biaoshi="100" mingCheng="审核状态" zhengXingDaiMa='${shenHeZhuangTai}' /> </td>
上一篇: 8款Chrome插件让你的新标签页更酷炫