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

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 --获取市  和 获取 县
时间: 2024-10-09 23:47:29

封装标签省,市,县。三级联动的相关文章

html实现 省——市——区三级联动

html实现  省--市--区三级联动 html中实现三级联动是一个不错的demo,博主在这里跟大家分享一下实现的过程,以及自己在过程中出现的一些问题,仅供参考. 首先我们将全国的省市区数据导入进来,这里已经直接整理搜集好了.如下: var provinceList = [ {name:'北京', cityList:[ {name:'市辖区', areaList:['东城区','西城区','崇文区','宣武区','朝阳区','丰台区','石景山区','海淀区','门头沟区','房山区','通州区

ajax(省,市,县)三级联动

下面我们用Jquery,ajax,做一个省,市,县的三级联动: 下面是我做三级联动下拉的步骤以及逻辑 第一步:先做一个省市区表格 第二步:建个PHP页面显示用我是在<body>里放<div>用来接收要显示的省市区表格信息,里面嵌入jquery-1.11.2.min.js和自己封装的三联动省市区的方法 第三步:写封装方法用JS 第四步:做个纯php处理页面,这个页面处理传过来的任何代号 首先我们要建立数据库: 这就是包含省,市,县的数据库, 下面我们就写主页面:sanji.php:

显示省、市、县三级联动的选择

本文主要是实现:用户交互,显示省市县三级联动的选择 代码: #!/usr/bin/env python# -*- coding:utf-8 -*-# author by lh dic = { '河北': { '石家庄': ['鹿泉', '藁城', '元氏'], '邯郸': ['永年', '涉县', '磁县'], }, '河南': { '郑州':['中原区','二七区','金水区'], '开封':['龙亭区','鼓楼区','禹王台区'] }, '山西': { '太原':['迎泽区','杏花岭区',

UIPickerView实现省 市 区三级联动

前几天用UIPickerView实现了一下三级联动具体的如下图 下面是实现方式 #import "ViewController.h" #define FirstComponent 0 #define SubComponent 1 #define ThirdComponent 2 @interface ViewController () @property(nonatomic,retain)NSDictionary* dict; @property(nonatomic,retain)NS

JQUERY省、市、县城市联动选择

JQUERY 插件开发——CITYLINKAGE(省.市.县城市联动选择) 第一部分:背景   开发源于需求,本次城市联动选择插件算是我写插件的一个特例吧,不是我目前工作需要些的,算是兴趣驱使吧.之前呢,一直想写这个插件,然后错过了一个写这个插件的机会(这个得回顾到很久以前了...此处省去N个字).然后最近“瘾”又犯了,呵呵,随手就拿这个“欠”了很久的插件开刀了.大家都应该知道“某宝”的这个插件写的还是很强大的,支持到街道(镇)级别...可见他们维护的前端数据有多大...不过呢,临渊羡鱼,不如退

JS实现全国三级联动

//引用js文件area.js <script src="/javascripts/area.js" type="text/javascript"></script> <li><label>全国 省 市 县 三级级联:</label> <i>*</i> <select id="s_province" name="s_province" c

Android开发之解析XML并实现三级联动效果

请尊重他人的劳动成果,转载请注明出处:Android开发之解析XML并实现三级联动效果 本实例主要应用XmlPullParser解析XML文档中的省市区,然后将数据绑定到Spinner上实现三级联动的效果.关于XmlPullParser的详解大家可以参考<Android开发之使用PULL解析和生成XML>一文. 运行效果图: 程序代码: 核心代码: <pre name="code" class="java">package com.jph.s

省、市、区(县)三级联动

用纯js写,这种方法的优点是可以随时引用该js文件,只需要引入该js文件即可. 1.写一个显示页面 sanji.php 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999

城市三级联动 AJAX-原生js封装

话不多说我们先来一张效果图给大家看一下: html代码如下: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>城市三级联动 - citys-原生js封装</title> <link rel="shortcut icon" href="../public/image/favi