java解析中國行政區域並在頁面顯示實現動態逐級篩選


一、實現目標

首先會有一個存放中國行政區域數據的一個txt文件,用java讀取並解析出來,並在頁面上通過下拉框的形式展示出來。實現效果如下圖,當選擇完省份后,在選擇該省份下的城市,然后在選擇該城市下的縣區這樣逐級顯示:

 

二、代碼實現:

1. 先創建一個javaBean,用來存放基本數據;

 1 public class Area {
 2     private String code ;//行政編碼
 3     private String name;//名稱
 4     private int level;//行政級別 0:省/直轄市 1:地級市 2:縣級市
 5     private String parentCode;//上一級的行政區划代碼
 6      
 7     public Area() {
 8         super();
 9     }
10      
11      public Area(String code, String name, int level, String parentCode) {
12          super();
13          this.code = code;
14          this.name = name;
15          this.level = level;
16          this.parentCode = parentCode;
17      }
18      
19      public String getCode() {
20          return code;
21      }
22      
23      public void setCode(String code) {
24          this.code = code;
25      }
26      
27      public String getName() {
28          return name;
29      }
30      
31      public void setName(String name) {
32          this.name = name;
33      }
34      
35      public int getLevel() {
36          return level;
37      }
38      
39      public void setLevel(int level) {
40          this.level = level;
41      }
42      
43      public String getParentCode() {
44          return parentCode;
45      }
46      
47      public void setParentCode(String parentCode) {
48          this.parentCode = parentCode;
49      }
50       
51      public String toString(){
52          return "行政編碼:"+this.getCode()+"\t名稱:"+this.getName()+"\t行政級別:"+
53                  this.getLevel()+"\t上一級的行政區划代碼:" +this.getParentCode();
54      }
55 }

2. 然后創建一個讀取txt資源文件的工具類;

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 import java.util.HashMap;
 8 import java.util.List;
 9 import java.util.Map;
10 
11 import com.***.Area;
12 
13 public class ReadAreaUtil {
14 
15     public  List<Area> provinceList = new ArrayList<Area>();
16     public  List<Area> townList = new ArrayList<Area>();
17     public  List<Area> countyList = new ArrayList<Area>();
18     
19     public  Map<String,List<Area>> getAreaData(String path){
20         ReadAllArea(path);
21         Map<String,List<Area>> result = new HashMap<String,List<Area>>();
22         result.put("provinceList", provinceList);
23         result.put("townList", townList);
24         result.put("countyList", countyList.subList(1, countyList.size())); //去掉表頭
25         
26         return result ;
27     }
28 
29     public  void ReadAllArea(String path){
30         String line = null;
31         BufferedReader reader = null;
32         File file = new File(path);
33          
34         String cityCode="";
35         String countyCode="";
36         try {
37         FileReader in = new FileReader(file);
38         reader = new BufferedReader(in);
39         //讀取文件的每一行
40         while((line = reader.readLine())!=null){
41             String[] data = cutString(line);
42             
43             //處理讀取的文件記錄
44             if(isProvince(data[0])){
45                 cityCode = data[0];
46                 Area area = new Area(data[0], data[1], 0, "0");
47                 provinceList.add(area);
48             }else if(isTown(data[0])){
49                 countyCode =data[0];
50                 Area area = new Area(data[0], data[1], 1, cityCode);
51                 townList.add(area);
52             }else{
53                 Area area = new Area(data[0], data[1], 2, countyCode);
54                 countyList.add(area);
55             }
56         }
57         } catch (FileNotFoundException e) {
58             e.printStackTrace();
59         } catch (IOException e) {
60             e.printStackTrace();
61         }finally{
62             try {
63                   reader.close();
64               } catch (IOException e) {
65                   e.printStackTrace();
66               }
67         }
68     }
69     
70     //字符分割
71      public  String[] cutString(String line){
72          String code="";
73          String name="";
74          code = line.substring(0, 6);
75          String lastStr = line.substring(7, line.length());
76          name = lastStr.substring(0, lastStr.indexOf("\t"));
77          String[] result = new String []{code,name};
78          return result;
79      }
80       
81     //判斷是否省或者直轄市
82      public  boolean isProvince(String code){
83          String last = code.substring(2);
84           if("0000".equalsIgnoreCase(last)){
85               return true;
86           }
87           return false;
88      }
89     //判斷是否地級市
90      public  boolean isTown(String code){
91          String last = code.substring(4);
92          if("00".equalsIgnoreCase(last)){
93              return true;
94          }
95          return false;
96      }
97      
98 }

3. 改項目使用了struts2,在action的方法中調用即可:

1         //讀txt數據...
2         String path = ServletActionContext.getServletContext().getRealPath("/2015Area.txt");
3         Map<String,List<Area>> area = new ReadAreaUtil().getAreaData(path);
4         List<Area> provinceList = area.get("provinceList");
5         ServletActionContext.getRequest().setAttribute("prlist", provinceList);

4. JSP頁面上的html,第一個select是直接遍歷的,但是后面兩個就要根據前面select選中的來動態顯示了,這里需要用到jQuery的Ajax;

 1 <div id="areaDiv" >
 2     <a >省份:
 3        <select id="sel_province" name="" >
 4           <option value="-1" >-請選擇-</option>
 5           <c:forEach var="pro" items="${prlist }">
 6                <option value="${pro.code }" >${pro.name }</option>
 7           </c:forEach>
 8        </select>
 9     </a>&nbsp;&nbsp;
10     <a class="wsy_f14">城市/區:
11        <select id="sel_town" name="" >
12          <option value="-1" >-----</option>
13        </select>
14      </a>&nbsp;&nbsp;
15      <a class="wsy_f14">縣級:
16          <select id="sel_county" name="" >
17             <option value="-1" >-----</option>
18           </select>
19      </a>
20  </div>

jQuery 代碼,select標簽變化時觸發函數並發送post請求數據;

 1 $(document).ready(function(){
 2     $('#sel_province').change(function(){
 3         var code = $(this).children('option:selected').val();//selected的值
 4         $.post("${basePath}ajax/filterArea.action",
 5             {
 6             parentCode:code,
 7             areaType:1//0省,1市,2縣
 8             },
 9             function(data){
10                 $("#sel_town").html(data);
11             });
12     });
13     
14     $('#sel_town').change(function(){
15         var code = $(this).children('option:selected').val();//selected的值
16         $.post("${basePath}ajax/filterArea.action",
17             {
18             parentCode:code,
19             areaType:2//0省,1市,2縣
20             },
21             function(data){
22                 $("#sel_county").html(data);
23             });
24     });
25 });

5. struts如何處理Ajax請求,專門寫一個action用於處理ajax請求;

 strut.xml:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
4     "http://struts.apache.org/dtds/struts-2.1.dtd">
5 <struts>
6     <package name="ajax" namespace="/ajax" extends="json-default">
7         <action name="filterArea"  method="filterArea" class="com.llw.action.AjaxFilter"></action>
8     </package>
9 </struts>

action:

 1 public class AjaxFilter extends ActionSupport {
 2 
 3     //行政區域過濾
 4     private String parentCode ;
 5     private int areaType ;
 6     public String getParentCode() {
 7         return parentCode;
 8     }
 9     public void setParentCode(String parentCode) {
10         this.parentCode = parentCode;
11     }
12     public int getAreaType() {
13         return areaType;
14     }
15     public void setAreaType(int areaType) {
16         this.areaType = areaType;
17     }
18     
19     public void filterArea(){
20         String htmlStr = "<option value=\"-1\">-請選擇-</option>" ;
21         
22         String path = ServletActionContext.getServletContext().getRealPath("/2015Area.txt");
23         Map<String,List<Area>> area = new ReadAreaUtil().getAreaData(path);
24         List<Area> areaList = null ;
25         if(areaType==1){ //0省,1市,2縣
26             areaList = area.get("townList");
27         }else if(areaType==2){
28             areaList = area.get("countyList");
29         }
30         if(areaList!=null && areaList.size()>0 && parentCode!=null){
31             for(Area a : areaList){
32                 if(parentCode.equals(a.getParentCode())){
33                     htmlStr += "<option value=\""+a.getCode()+"\">"+a.getName()+"</option>" ;
34                 }
35             }
36         }
37         
38 //        System.out.println("xxxxx"+htmlStr);
39         HttpServletResponse response = ServletActionContext.getResponse();
40         PrintWriter writer = null;
41         try {
42             writer = response.getWriter();
43         } catch (IOException e) {
44             e.printStackTrace();
45         }
46         writer.print(htmlStr);  
47         writer.flush();  
48         writer.close();
49     }
50 
51 }

 

三、總結:以上基本完成了需要的效果,用java讀取txt資源文件,並封裝到List里面,上面代碼中把不同級別的數據放到不同的list里了,也可以放到同一個list里面,這個根據自己需要可以去修改;然后是通過Ajax方式動態逐級顯示select展示的內容;並演示了strut中如何處理ajax請求。上面展示了其中一種strut處理ajax請求的方式,還有另外一中方式,跟普通action的原理類似,就是通過action里定義的成員變量,JS里的回調函數訪問這些成員變量;

這里貼一下第二種方式的使用例子:

strut.xml:
<action name="testAjax" class="com.crm.action.Ajaxcustomer" method="sayHello">
    <result type="json" name="success"></result>
</action> 

action:
public class AjaxFilter extends ActionSupport {
    private int type;
    private String phone;
    private Map<String,Object> map ;
    //…get/set方法這里省略
    
    public String sayHello(){
        map = new HashMap<String, Object>();
        map.put("aaa", "abc");
        
        return SUCCESS;
    }    
}    

JS:
function test(){
    $.post("ajax/testAjax.action",//請求目標
        {
        type:1,
        phone:$("#phone").val() },
        function(data) {//回調函數
            alert(data.map.aaa);
            $("#lia1").html(data.map.paramNum);//設置頁面標簽值
        });
}

 

 

四、說明:以上java讀取txt資源文件的方法來自網絡整理修改,后面的內容是項目中實際應用,現記以溫之;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM