封装标签省,市,县。三级联动


package
com.jy.modules.tld; import com.jy.platform.api.sysarea.SysAreaAPI; import java.io.IOException; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import org.springframework.context.ApplicationContext; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class ConsultingComListTag extends TagSupport { private static final long serialVersionUID = 1L; private String pid = "pid"; private String pname = ""; private String cid = "cid"; private String cname = ""; private String tid = "tid"; private String tname = ""; private int level = 3; private String type = "select"; private String defaultValue = ""; private String className = ""; private String onchange; private String emptyText = "--请选择--"; private boolean hasBlank = true; private String extendProperty = ""; public int doStartTag() throws JspException { StringBuffer select = new StringBuffer(); try { //获取实现接口 WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); SysAreaAPI sysAreaAPI = (SysAreaAPI)context.getBean("sysAreaAPI"); if (this.type.equals("select")) { String defaultPvalue = ""; String defaultCvalue = ""; String defaultTvalue = ""; List plist = sysAreaAPI.queryChildAreaByPid("0");//省结果集 List clist = null; List tlist = null; if (this.level == 1) { defaultPvalue = this.defaultValue; } else if (this.level == 2) { if ((this.defaultValue != null) && (this.defaultValue.length() > 0)) { defaultCvalue = this.defaultValue; defaultPvalue = sysAreaAPI.queryParentCodeByCode(defaultCvalue); clist = sysAreaAPI.queryChildAreaByCode(defaultPvalue); } } else if ((this.level == 3) && (this.defaultValue != null) && (this.defaultValue.length() > 0)) { defaultTvalue = this.defaultValue; defaultCvalue = sysAreaAPI.queryParentCodeByCode(defaultTvalue); defaultPvalue = sysAreaAPI.queryParentCodeByCode(defaultCvalue); clist = sysAreaAPI.queryChildAreaByCode(defaultPvalue); tlist = sysAreaAPI.queryChildAreaByCode(defaultCvalue); } select.append("<select "); if (this.level != 1) { if ((this.cid != null) && (this.cid.length() > 0)) { select.append(" title='"); select.append(this.cid); if ((this.level == 3) && (this.tid != null) && (this.tid.length() > 0)) { select.append(","); select.append(this.tid); } select.append("'"); } select.append(" onchange='loadArea(this);' "); } select = getProvinceProp(select); select.append(">"); if (this.hasBlank) { select.append("<option value=''>"); select.append(this.emptyText); select.append("</option>"); } if ((plist != null) && (plist.size() > 0)) { for (int i = 0; i < plist.size(); i++) { select.append("<option value='"); select.append(((Map)plist.get(i)).get("AREACODE")); select.append("' "); if (((Map)plist.get(i)).get("AREACODE").equals(defaultPvalue)) { select.append(" selected='selected'"); } select.append(">"); select.append(((Map)plist.get(i)).get("AREANAME")); select.append("</option>"); } } select.append("</select>"); if (this.level >= 2) {// select.append("<select "); if (this.level != 2) { if ((this.cid != null) && (this.cid.length() > 0)) { select.append(" title='" + this.tid + "'"); } select.append(" onchange='loadArea(this);' "); } select = getCityProp(select); select.append(">"); if (this.hasBlank) { select.append("<option value=''>"); select.append(this.emptyText); select.append("</option>"); } if ((clist != null) && (clist.size() > 0)) { for (int i = 0; i < clist.size(); i++) { select.append("<option value='"); select.append(((Map)clist.get(i)).get("AREACODE")); select.append("'"); if (((Map)clist.get(i)).get("AREACODE").equals(defaultCvalue)) { select.append(" selected='selected' "); } select.append(">"); select.append(((Map)clist.get(i)).get("AREANAME")); select.append("</option>"); } } select.append("</select>"); } if (this.level == 3) {// select.append("<select "); select = getTownProp(select); select.append(">"); if (this.hasBlank) { select.append("<option value=''>"); select.append(this.emptyText); select.append("</option>"); } if ((tlist != null) && (tlist.size() > 0)) { for (int i = 0; i < tlist.size(); i++) { select.append("<option value='"); select.append(((Map)tlist.get(i)).get("AREACODE")); select.append("'"); if (((Map)tlist.get(i)).get("AREACODE").equals(defaultTvalue)) { select.append(" selected='selected' "); } select.append(">"); select.append(((Map)tlist.get(i)).get("AREANAME")); select.append("</option>"); } } select.append("</select>"); } } else if (this.type.equals("text")) { String text = ""; if ((this.defaultValue != null) && (this.defaultValue.length() > 0)) { Map map = sysAreaAPI.getAreaByCode(this.defaultValue); if ((map.get("PARENTID") != null) && (("" + map.get("PARENTID")).length() > 0) && (!("" + map.get("PARENTID")).equals("0"))) { Map temp = sysAreaAPI.getAreaByCode("" + map.get("PCODE")); select.append(temp.get("PNAME")); select.append(" "); } if (map.get("PNAME") != null) { select.append(map.get("PNAME")); select.append(" "); } select.append(map.get("AREANAME")); } select.append(text); } } catch (Exception e) { e.printStackTrace(); } try { this.pageContext.getOut().write(select.toString()); } catch (IOException e) { e.printStackTrace(); } return 0; } private StringBuffer getProvinceProp(StringBuffer results) throws JspException { prepareAttribute(results, "id", getPid()); prepareAttribute(results, "name", getPname()); prepareAttribute(results, "className", getClassName()); return results; } private StringBuffer getCityProp(StringBuffer results) throws JspException { prepareAttribute(results, "id", getCid()); prepareAttribute(results, "name", getCname()); prepareAttribute(results, "className", getClassName()); return results; } private StringBuffer getTownProp(StringBuffer results) throws JspException { prepareAttribute(results, "id", getTid()); prepareAttribute(results, "name", getTname()); prepareAttribute(results, "className", getClassName()); return results; } protected void prepareAttribute(StringBuffer handlers, String name, Object value) { if (value != null) { handlers.append(" "); handlers.append(name); handlers.append("=\""); handlers.append(value); handlers.append("\""); } } public String getPid() { return this.pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return this.pname; } public void setPname(String pname) { this.pname = pname; } public String getCid() { return this.cid; } public void setCid(String cid) { if (null == cid) this.cid = "cid"; else this.cid = cid; } public String getCname() { return this.cname; } public void setCname(String cname) { this.cname = cname; } public String getTid() { return this.tid; } public void setTid(String tid) { if (null == tid) this.tid = "tid"; else this.tid = tid; } public String getTname() { return this.tname; } public void setTname(String tname) { this.tname = tname; } public int getLevel() { return this.level; } public void setLevel(int level) { if (level > 3) this.level = 3; else this.level = level; } public String getType() { return this.type; } public void setType(String type) { if (null == type) this.type = "select"; else this.type = type; } public String getDefaultValue() { return this.defaultValue; } public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; } public String getClassName() { return this.className; } public void setClassName(String className) { this.className = className; } public String getOnchange() { return this.onchange; } public void setOnchange(String onchange) { this.onchange = onchange; } public String getEmptyText() { return this.emptyText; } public void setEmptyText(String emptyText) { if (null == emptyText) this.emptyText = "==请选择=="; else this.emptyText = emptyText; } public boolean isHasBlank() { return this.hasBlank; } public void setHasBlank(boolean hasBlank) { this.hasBlank = hasBlank; } public String getExtendProperty() { return this.extendProperty; } public void setExtendProperty(String extendProperty) { this.extendProperty = extendProperty; } }



<?xml version="1.0" encoding="UTF-8"?>
<!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</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>sysarea</short-name>
	<uri>/sysarea</uri>
	<description>
        <![CDATA["城市信息自定义标签"]]>
	</description>
	<tag>
		<name>search</name>
		<tag-class>com.jy.modules.tld.ConsultingComListTag</tag-class>
		<body-content>JSP</body-content>
		
		<attribute>
			<name>pid</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>pname</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>cid</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>cname</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>tid</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>tname</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>level</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>type</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>defaultValue</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>className</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>onchange</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>emptyText</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>hasBlank</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>extendProperty</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		
	</tag>
</taglib>

  

/**
 *@description 生成省-市-区县三级联动下拉列表
 *@author lin
 *@date 20141023 20:19
 * eg: 在页面初始化方法中调用 $.jyarea.init({divId:'test',level:3,defaultVal:['110000','110100','110101']});;        
 * 说明:test为所属DIV容器的ID
 *        level值为是2级展示还是3级展示,默认3级
 *     defaultVal:默认初始数组[省,市,县],值为cityCode,且顺序不能更改
 */

function loadArea(obj){
    var array = obj.title.split(',');
    for(var i=0;i<array.length;i++){
        $('#' + array[i]).html('<option value="">--请选择--</option>');
    }
    var areaCode = "";
    if(obj.value != null && obj.value != ""){
        areaCode = obj.value;
        $.ajax({
            url: contextRootPath + '/sysArea/queryAreaByCode/' +areaCode ,
            type: 'POST',
            async: false,
            dataType: 'json',
            data:'',
            error: function() {
                jyDialog({"type":"error"}).alert("获取城市列表错误!");
                //alert('获取城市列表错误');
            },
            success: function(result) {
                var areas = result.data;
                for (var j = 0; j <= areas.length - 1; j++) {
                    $('#'+obj.title).append("<option value='"+areas[j].AREACODE+"'>"+areas[j].AREANAME+"</option>");
                }
            }
        });
    }
    
    
}



//页面导入 在jsp tld 目录下面
sysarea.tld

<%@taglib prefix="sysarea" uri="/sysarea" %>


<tr>
<th> 省、市、区 :</th>
<td colspan="6">
<sysarea:search level="3" pid="pAreacode" pname="pAreacode" cid="cAreacode" cname="cAreacode" tname="areacode" tid="areacode" type="select" />
</td>
</tr>

 
import java.util.List;
import java.util.Map;

public abstract interface SysAreaAPI
{
public abstract List<Map> queryChildAreaByCode(String paramString); //市 / 区
public abstract List<Map> queryChildAreaByPid(String paramString);//省
public abstract String queryParentCodeByCode(String paramString);

 

public abstract Map getAreaByCode(String paramString);
}
SELECT area_code AREACODE, area_name AREANAME FROM sys_area WHERE parent_id = 0 order by area_code --获取省 
    
SELECT area_code AREACODE, area_name AREANAME       FROM sys_area     WHERE parent_id = (SELECT ID FROM sys_area WHERE area_code = '130100') order by area_code --获取市  和 获取 县

 

posted @ 2015-06-09 16:55  也许还年轻  阅读(248)  评论(0编辑  收藏  举报