上篇博客我们简单的创建了一个自定义标签,这篇博客我们将继续介绍自定义JSP标签,不过难度升级,我们来定义一个稍难一点的自定义JSP标签,来做一个下拉框,查询父节点对应下面的子节点,我们以查询一个省份下对应的城市为例。
还是按照之前的步骤,首先我们先创建一个处理该标签的类。
<span style="font-size:12px;">package com.csds.area;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.zlwy.common.util.SpringUtils;
import com.zlwy.organ.basedict.manager.IBaseDictManager;
import com.zlwy.organ.basedict.service.impl.AreaInfo;
import com.zlwy.organ.entity.BaseDict;
public class AreaInfoTagTest extends TagSupport{
public String areaSelectValue;//选择值
public String areaParentId;//父id
/**
* <p>Description: 结束标签时调用</p>
*/
public int doEndTag() throws JspException {
StringBuffer sb = getAreaInfoName();
JspWriter out = pageContext.getOut();
try {
out.print(sb.toString());
} catch (IOException ex) {
throw new JspTagException("IOException: " + ex.toString());
}
return super.doEndTag();
}
/**
* <p>Description: 根据父id查询子节点名称</p>
*/
private StringBuffer getAreaInfoName(){
StringBuffer sb = new StringBuffer();
sb.append("<option value=\"\">请选择</option>");
List<AreaInfo> areaInfoList = new ArrayList<AreaInfo>();
try {
areaInfoList = areaInfoManager.getDictBySSuperGuid(areaParentId);
} catch (Exception e) {
e.printStackTrace();
}
if (areaInfoList != null && areaInfoList.size()>0) {
for (AreaInfo areaInfo : areaInfoList) {
if (areaSelectValue.trim().equals(areaInfo.getSGuid().trim())) {
sb.append("<option value =\"" + areaInfo.getSGuid().trim()+ "\" selected=\"selected\">");
sb.append("" + areaInfo.getAreaName() + "</option>");
} else {
sb.append("<option value =\"" + areaInfo.getSGuid().trim()+ "\">");
sb.append(""+ areaInfo.getAreaName() + "</option>");
}
}
}
return sb;
}
/**
* <p>Description: sql语句查询,根据父id查询子节点名称</p>
*/
public List<AreaInfo> getAreaInfoNameByParentId(String areaParentId){
List<AreaInfo> areaInfoList = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("from AreaInfo a where a.areaParentId = '" + areaParentId + "'");
sql.append("order by t.IOrderNo asc");
areaInfoList = getList(sql.toString());
} catch(Exception e) {
e.printStackTrace();
throw e;
}
return areaInfoList;
}
/*********************************get/set方法****************************/
public String getAreaSelectValue() {
return areaSelectValue;
}
public void setAreaSelectValue(String areaSelectValue) {
this.areaSelectValue = areaSelectValue;
}
public String getAreaParentId() {
return areaParentId;
}
public void setAreaParentId(String areaParentId) {
this.areaParentId = areaParentId;
}
}</span>
创建一个xxx.tld文件,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>mt</short-name>
<tag>
<description> Display areaInfo name </description>
<name>areaInfolist</name>
<tag-class>com.csds.area.AreaInfoTag</tag-class>
<attribute>
<name><span style="font-size:12px;">areaParentId</span></name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name><span style="font-size:12px;">areaSelectValue</span></name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在web.xml中的配置一样
<taglib>
<taglib-uri>/MyTagTest</taglib-uri>
<taglib-location>/WEB-INF/tlds/myTagTest.tld</taglib-location>
</taglib>
然后在页面中引入如下代码
<span style="font-size:14px;"><%@ taglib uri="/MyTagTest" prefix="mt"%> </span>
最后在页面中直接用我们自定义的JSP标签就可以了
<span style="font-size:12px;"><select>
<mt:areaInfolist areaParentId="" areaSelectValue=""></zw:basedictlist>
</select></span>
这样,一个自定义的JSP标签就可以用了。到这里,关于自定义标签的内容就介绍完了,这三篇文章分别从理论到实践,从简到难对自定义JSP标签进行了介绍。有兴趣的可以深入研究一下。