Easyui表單之下拉列表的三級聯動

1、實現三級聯動須要鏈接數據庫javascript

2、須要JSON數據的解析css

3、須要Servlet類與界面相對應值的傳遞html

1. 界面層須要的代碼以下:java

<!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 class="easyui-combobox"data-options="url:'json/combo.json',
valueField:'id',textField:'text',
editable:false">

<br><br>
省:<input id="sheng" class="easyui-combobox" style="width:100px"
data-options="{
url:'RegionServlet?parentid=0',
valueField:'regionID',
textField:'regionName',
onSelect:function(region)
{
    //alert('選擇了省id='+region.regionID);
    $('#shi').combobox('clear');// 清除原有項目
    $('#quxian').combobox('clear');// 清除原有顯示項目
    // 從新加載
    $('#shi').combobox('reload','RegionServlet?parentid='+region.regionID);
    $('#quxian').combobox('reload','RegionServlet'); //清理原有下拉項目
    
}
}">
市:<input id="shi" class="easyui-combobox" style="width:100px"
data-options="{
url:'RegionServlet',
valueField:'regionID',
textField:'regionName',
onSelect:function(region)
{
    $('#quxian').combobox('clear');// 清除原有項目
    // 從新加載
    $('#quxian').combobox('reload','RegionServlet?parentid='+region.regionID);
}

}">
區縣:<input id="quxian" class="easyui-combobox" style="width:100px"
data-options="{
url:'RegionServlet',
valueField:'regionID',
textField:'regionName'
}">
</body>
</html>

2. 經過hibernate建立實體類與映射文件jquery

實體類:數據庫

package com.hanqi.entity;

// 地區實體類
public class Region {

    // 地區ID
    private String regionID;
    
    // 地區名稱
    private String regionName;
        
    // 上級地區ID
    private String parentRegionID;

    public String getRegionID() {
        return regionID;
    }

    public void setRegionID(String regionID) {
        this.regionID = regionID;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public String getParentRegionID() {
        return parentRegionID;
    }

    public void setParentRegionID(String parentRegionID) {
        this.parentRegionID = parentRegionID;
    }
    
    
}

3.DAO層代碼:json

package com.hanqi.dao;

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

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.hanqi.entity.Region;

public class RegionDAO {


    Configuration cfg = null;
    ServiceRegistry sr =null;
    SessionFactory sf =null;
    Session se=null;
    Transaction tr=null;
    public RegionDAO()
    {
        //加載配置文件
        cfg = new Configuration().configure();
        //註冊服務
        sr = new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties())
                .build();
    }
    // 初始化
    private void init()
      {
          sf=cfg.buildSessionFactory(sr);
          se= sf.openSession();
          tr=se.beginTransaction();
      }
    // 提交和釋放
     private void destory()
      {
              tr.commit();
            se.close();
            sf.close();
      }
     
     // 獲取數據列表
     
     public List<Region> getList(String parentid)
     {
         
         List<Region> rtn = new ArrayList<Region>();
         
         init();
         
         // 查詢數據
         rtn=se.createQuery("from Region where parentRegionID = ?")
                 .setString(0, parentid).list();
         
         destory();
         
         return rtn;
     }
}

4.Service層代碼:app

package com.hanqi.service;

import java.util.List;

import com.hanqi.dao.RegionDAO;
import com.hanqi.entity.Region;

public class RegionService {
    
    public List<Region> getList(String parentid)
    {
        return new RegionDAO().getList(parentid);
    }

}

5.Servlet層代碼:(解析JSON數據的jar包FastJson)ui

package com.hanqi.controller;

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.hanqi.entity.Region;
import com.hanqi.service.RegionService;

public class RegionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public RegionServlet() {
        super();
       
    }

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");
            
            // 接收參數
            String parentid = request.getParameter("parentid");
            
            if(parentid!=null)
            {
            
            // 讀取地區表明
            List<Region> lr = new RegionService().getList(parentid);
            ;
            // 返回JSON格式的數據
            String json = JSONArray.toJSONString(lr);
            
            response.getWriter().write(json);
            }
            else
            {
                response.getWriter().write("[]");
            }
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        doGet(request, response);
    }

}
相關文章
相關標籤/搜索