通過數據庫和EasyUI的combobox級聯實現省市區三級聯動


1.新建一個web項目

2.因為這里用到了數據庫所以我們在lib目錄導入Hibernate的jar包、fastjson.jar包及數據庫jar包

3.同樣導入EasyUI的組件配置,並在新建的html<head></head>中引用

<!-- 1    jQuery的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.min.js"></script>

<!-- 2    css資源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/default/easyui.css">

<!-- 3    圖標資源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/icon.css"> 

<!-- 4    EasyUI的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
 
<!-- 5    本地語言 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>

4.在src目錄下一次建四個包

5.新建一個實體類Area,並生成set和get方法

package com.hh.entity;

public class Area {

    private String areaID ; //區域id
    
    private String areaName ; // 區域名稱
    
    private String parentID ; // 父級id

    public String getAreaID() {
        return areaID;
    }

    public void setAreaID(String areaID) {
        this.areaID = areaID;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getParentID() {
        return parentID;
    }

    public void setParentID(String parentID) {
        this.parentID = parentID;
    }
    
    
}

 6.在dao層建一個類,獲取所有的列表

package com.hh.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.hh.entity.Area;

public class AreaDAO {

    Configuration cfg = null ;
    ServiceRegistry sr = null ;
    SessionFactory sf = null ; 
    Session se = null ;
    Transaction ts = null ;
    
    //構造方法    
    public AreaDAO()
    {
        //1獲取配置文件 
        cfg = new Configuration().configure() ;
        
        //2注冊配置  
        sr = new StandardServiceRegistryBuilder().
                                            applySettings(cfg.getProperties()).build();
        
    }
    
    private void init()
    {
        //3獲取SessionFactory
        sf = cfg.buildSessionFactory(sr) ;
        
        //4產生Session
        se =sf.openSession() ;
        
        //5啟動事務
        ts = se.beginTransaction() ;
    }
    
    private void destroy()
    {
        ts.commit();  //提交事務
        
        se.close() ;//關閉釋放資源
        
        sf.close();//關閉釋放資源
    }
    
    //    獲得所有區域列表
    public List<Area> showAll(String areaID) 
    {
        List<Area> list = new ArrayList<Area>() ;
        
        init() ;
        
        Query qu = se.createQuery("from Area where parentID = ? ") ;
        
        qu.setString(0, areaID) ;
        
        list = qu.list() ;
        
        destroy();
        
        return list;
    }
    
    
    
}

 7.在service層建一個類,用來調用基層數據庫dao中的方法

package com.hh.service;

import java.util.List;

import com.hh.dao.AreaDAO;
import com.hh.entity.Area;

public class AreaService
{
        public List<Area> showAll(String areaID)
        {
            return new AreaDAO().showAll(areaID) ;
        }
}

 8.在servlet包中建一個servlet

package com.hh.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.hh.entity.Area;
import com.hh.service.AreaService;

/**
 * Servlet implementation class AreaServlet
 */
public class AreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AreaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //        轉碼
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        
        // 接受參數
        String areaID = request.getParameter("areaid") ;
        
        List<Area> list = new AreaService().showAll(areaID) ;
        
        String json = JSONArray.toJSONString(list) ;
        
        response.getWriter().write(json);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

9.html部分

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 1    jQuery的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.min.js"></script>

<!-- 2    css資源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/default/easyui.css">

<!-- 3    圖標資源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/icon.css"> 

<!-- 4    EasyUI的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
 
<!-- 5    本地語言 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>

</head>
<body><input id="sheng" style="width:200px" 
                class="easyui-combobox" data-options="    
                                                                                missingMessage:'請選擇省份',
                                                                                url:'AreaServlet?areaid=0',
                                                                                valueField:'areaID',
                                                                                textField:'areaName',
                                                                                onSelect:function(area){
                                                                                $('#shi').combobox('clear');
                                                                                $('#qu').combobox('clear');
                                                                                $('#shi').combobox('reload','AreaServlet?areaid='+area.areaID),
                                                                                $('#qu').combobox('reload','AreaServlet')}" /><input id="shi" class="easyui-combobox " 
                                data-options="missMessage:'請選擇城市' ,
                                                        url:'AreaServlet' ,
                                                        valueField:'areaID',
                                                                                textField:'areaName',
                                                                                onSelect:function(area){
                                                                                $('#qu').combobox('clear');
                                                                                //alert(area.regionID);
                                                                                $('#qu').combobox('reload','AreaServlet?areaid='+area.areaID)
                                                                                }"/><input id="qu"     class="easyui-combobox "         
                                data-options="missMessage:'請選擇區縣' ,
                                url:'AreaServlet' ,
                                valueField:'areaID' ,
                                textField:'areaName'" />
</body>
</html>

10.運行servlet,Hibernate正向工程生成表,並填充測試數據

 


免責聲明!

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



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