Ajax 案例之三级联动

  每次在博客园网站写博客,格式真的好难搞,还望好心人告知更好的编辑工具。接下来进入正题:三级联动(其效果演示可看我的博文 Ajax 学习总结 末尾)。

  1. 数据表设计(Oracle)
    1. 新建数据表 Employees(员工信息)、Locations(城市信息)、Departments(部门信息),其中 Departments 表的外键为 locations 表的 location_id ,Employees 表的外键为 Departments 表Department_id

  2. DAO 层设计(c3p0 数据库连接池、JDBCUtils)

    a. 获取释放数据库连接的工具类:JDBCTools

  1.      package com.javaweb.userajax.serlet.list.show.tools;  
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  3. import javax.sql.DataSource;  
  4. import java.sql.Connection;  
  5. import java.sql.SQLException;  
  6. /** 
  7.  * Created by shkstart on 2017/12/05. 
  8.  */  
  9. public class JDBCTools {  
  10.     private static DataSource dataSource;  
  11.     static {  
  12.         dataSource = new ComboPooledDataSource("listShow");  
  13.     }  
  14.     public static Connection getConnection() {  
  15.         Connection connection = null;  
  16.         try {  
  17.             connection = dataSource.getConnection();  
  18.         } catch (SQLException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         return connection;  
  22.     }  
  23.     public void releaseConnection(Connection connection) {  
  24.         try {  
  25.             connection.close();  
  26.         } catch (SQLException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30. }  

    b. 获取某张数据表的所有数据以及根据限定的查询条件获取部分值的 DAO 类:DAO

  31. package com.javaweb.userajax.serlet.list.show.dao;  
  32. import com.javaweb.userajax.serlet.list.show.tools.JDBCTools;  
  33. import org.apache.commons.dbutils.QueryRunner;  
  34. import org.apache.commons.dbutils.handlers.BeanHandler;  
  35. import org.apache.commons.dbutils.handlers.BeanListHandler;  
  36. import java.lang.reflect.ParameterizedType;  
  37. import java.lang.reflect.Type;  
  38. import java.sql.Connection;  
  39. import java.sql.SQLException;  
  40. import java.util.List;  
  41. /** 
  42.  * Created by shkstart on 2017/12/05. 
  43.  */  
  44. public class DAO<T> {  
  45.     private QueryRunner queryRunner;  
  46.     private Class<T> type;  
  47.     public DAO() {  
  48.         queryRunner = new QueryRunner();  
  49.         Type superClass = getClass().getGenericSuperclass();  
  50.         if (superClass instanceof ParameterizedType) {  
  51.             ParameterizedType parameterizedType = (ParameterizedType) superClass;  
  52.             Type[] args = parameterizedType.getActualTypeArguments();  
  53.             if (args != null && args.length > 0) {  
  54.                 if (args[0] instanceof Class) {  
  55.                     type = (Class<T>) args[0];  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60.     public List<T> getAll(String sql) {  
  61.         Connection connection = JDBCTools.getConnection();  
  62.         List<T> list = null;  
  63.         try {  
  64.             list = queryRunner.query(connection, sql, new BeanListHandler<T>(type));  
  65.         } catch (SQLException e) {  
  66.             e.printStackTrace();  
  67.         }  
  68.         return list;  
  69.     }  
  70.     public List<T> getList(String sql, Object ... args) {  
  71.         Connection connection = JDBCTools.getConnection();  
  72.         List<T> list = null;  
  73.         try {  
  74.             list = queryRunner.query(connection, sql, new BeanListHandler<T>(type), args);  
  75.         } catch (SQLException e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.         return list;  
  79.     }  
  80.     public T getValue(String sql, Object ... args) {  
  81.         Connection connection = JDBCTools.getConnection();  
  82.         T entity = null;  
  83.         try {  
  84.             entity = queryRunner.query(connection, sql, new BeanHandler<T>(type), args);  
  85.         } catch (SQLException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         return entity;  
  89.     }  
  90. }  

    c. 根据案例需求定义数据表对应的 dao 接口

    DepartmentsDao:

  91. package com.javaweb.userajax.serlet.list.show.dao;  
  92. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  93. import java.util.List;  
  94. /** 
  95.  * Created by shkstart on 2017/12/06. 
  96.  */  
  97. public interface DepartmentsDao {  
  98.     List<Departments> getAll(Integer locationId);  
  99. }  

    EmployeesDao:

  100. package com.javaweb.userajax.serlet.list.show.dao;  
  101. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  102. import java.util.List;  
  103. /** 
  104.  * Created by shkstart on 2017/12/06. 
  105.  */  
  106. public interface EmployeesDao {  
  107.     List<Employees> getAll(Integer departmentId);  
  108.     Employees getEmp(Integer employeeId);  
  109. }  

    LocationsDao:

  110. package com.javaweb.userajax.serlet.list.show.dao;  
  111. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  112. import java.util.List;  
  113. /** 
  114.  * Created by shkstart on 2017/12/06. 
  115.  */  
  116. public interface LocationsDao {  
  117.     List<Locations> getAll();  
  118. }  

    d. 根据数据表编写对应的 domain 类

    Departments:

  119. package com.javaweb.userajax.serlet.list.show.domain;  
  120. /** 
  121.  * Created by shkstart on 2017/12/06. 
  122.  */  
  123. public class Departments {  
  124.     private Integer departmentId;  
  125.     private String departmentName;  
  126.     private Integer locationId;  
  127.     public Integer getDepartmentId() {  
  128.         return departmentId;  
  129.     }  
  130.     public void setDepartmentId(Integer departmentId) {  
  131.         this.departmentId = departmentId;  
  132.     }  
  133.     public String getDepartmentName() {  
  134.         return departmentName;  
  135.     }  
  136.     public void setDepartmentName(String departmentName) {  
  137.         this.departmentName = departmentName;  
  138.     }  
  139.     public Integer getLocationId() {  
  140.         return locationId;  
  141.     }  
  142.     public void setLocationId(Integer locationId) {  
  143.         this.locationId = locationId;  
  144.     }  
  145.     public Departments(){  
  146.     }  
  147.     @Override  
  148.     public String toString() {  
  149.         return "Departments{" +  
  150.                 "departmentId=" + departmentId +  
  151.                 ", departmentName='" + departmentName + '\'' +  
  152.                 ", locationId=" + locationId +  
  153.                 '}';  
  154.     }  
  155. }  

    Employees:

  156. package com.javaweb.userajax.serlet.list.show.domain;  
  157. /** 
  158.  * Created by shkstart on 2017/12/06. 
  159.  */  
  160. public class Employees {  
  161.     private Integer employeeId;  
  162.     private Integer departmentId;  
  163.     private String lastName;  
  164.     private String email;  
  165.     @Override  
  166.     public String toString() {  
  167.         return "Employees{" +  
  168.                 "employeeId=" + employeeId +  
  169.                 ", departmentId=" + departmentId +  
  170.                 ", lastName='" + lastName + '\'' +  
  171.                 ", email='" + email + '\'' +  
  172.                 '}';  
  173.     }  
  174.     public Integer getDepartmentId() {  
  175.         return departmentId;  
  176.     }  
  177.     public void setDepartmentId(Integer departmentId) {  
  178.         this.departmentId = departmentId;  
  179.     }  
  180.     public Integer getEmployeeId() {  
  181.         return employeeId;  
  182.     }  
  183.     public void setEmployeeId(Integer employeeId) {  
  184.         this.employeeId = employeeId;  
  185.     }  
  186.     public String getLastName() {  
  187.         return lastName;  
  188.     }  
  189.     public void setLastName(String lastName) {  
  190.         this.lastName = lastName;  
  191.     }  
  192.     public String getEmail() {  
  193.         return email;  
  194.     }  
  195.     public void setEmail(String email) {  
  196.         this.email = email;  
  197.     }  
  198.     public Employees(){  
  199.     }  
  200. }  

    Locations:

  201. package com.javaweb.userajax.serlet.list.show.domain;  
  202. /** 
  203.  * Created by shkstart on 2017/12/06. 
  204.  */  
  205. public class Locations {  
  206.     private Integer locationId;  
  207.     private String city;  
  208.     public Integer getLocationId() {  
  209.         return locationId;  
  210.     }  
  211.     public void setLocationId(Integer locationId) {  
  212.         this.locationId = locationId;  
  213.     }  
  214.     public String getCity() {  
  215.         return city;  
  216.     }  
  217.     public void setCity(String city) {  
  218.         this.city = city;  
  219.     }  
  220.     public Locations() {  
  221.     }  
  222.     @Override  
  223.     public String toString() {  
  224.         return "Locations{" +  
  225.                 "locationId=" + locationId +  
  226.                 ", city='" + city + '\'' +  
  227.                 '}';  
  228.     }  
  229. }  

    e. 根据 DAO 类以及 domain类实现数据表对应的 dao 接口

    DepartmentsImpl:

  230. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  231. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  232. import com.javaweb.userajax.serlet.list.show.dao.DepartmentsDao;  
  233. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  234. import java.util.List;  
  235. /** 
  236.  * Created by shkstart on 2017/12/06. 
  237.  */  
  238. public class DepartmentsImpl extends DAO<Departments> implements DepartmentsDao {  
  239.     @Override  
  240.     public List<Departments> getAll(Integer locationId) {  
  241.         String sql = "SELECT department_id departmentId, department_name departmentName, location_id locationId FROM departments WHERE location_id=?";  
  242.         System.out.println(sql);  
  243.         List<Departments> departmentsList = getList(sql, locationId);  
  244.         return departmentsList;  
  245.     }  
  246. }  

    EmployeesImpl:

  247. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  248. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  249. import com.javaweb.userajax.serlet.list.show.dao.EmployeesDao;  
  250. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  251. import java.util.List;  
  252. /** 
  253.  * Created by shkstart on 2017/12/06. 
  254.  */  
  255. public class EmployessDaoImpl extends DAO<Employees> implements EmployeesDao {  
  256.     @Override  
  257.     public List<Employees> getAll(Integer departmentId) {  
  258.         String sql = "SELECT employee_id employeeId, last_name lastName FROM employees WHERE department_id=?";  
  259.         System.out.println(sql);  
  260.         List<Employees> employeesList = getList(sql, departmentId);  
  261.         return employeesList;  
  262.     }  
  263.     @Override  
  264.     public Employees getEmp(Integer employeeId) {  
  265.         String sql = "SELECT employee_id employeeId, department_id departmentId, last_name lastName, email FROM employees WHERE employee_id=?";  
  266.         Employees employees = getValue(sql, employeeId);  
  267.         return employees;  
  268.     }  
  269. }  

    LocationsImpl:

  270. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  271. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  272. import com.javaweb.userajax.serlet.list.show.dao.LocationsDao;  
  273. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  274. import java.util.List;  
  275. /** 
  276.  * Created by shkstart on 2017/12/06. 
  277.  */  
  278. public class LocationsDaoImpl extends DAO<Locations> implements LocationsDao {  
  279.     @Override  
  280.     public List<Locations> getAll() {  
  281.         String sql = "SELECT location_id locationId, city FROM locations";  
  282.         List<Locations> locationsList = getAll(sql);  
  283.         return locationsList;  
  284.     }  
  285. }  
    1. JSP 页面实现

      a. 我们先需要从 servlet 转发到 JSP 页面,将所有的 locations 信息封装在 request 中传回 jsp 页面在页面初始化的时候将所有 locations 信息显示

      index.jsp(案例所访问的页面,此页面直接跳转到 servlet,在 Servlet 中获取 location 信息后转发会显示页面)

  286. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  287. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  288. <html>  
  289. <head>  
  290.     <title>ForwardPage</title>  
  291. <body>  
  292. <h3></h3>  
  293. <jsp:forward page="getCity.do"/>  
  294. </body>  
  295. </html>  

    getCity 方法(servlet 中利用反射处理多个请求,前面的博文已经讲述过,此方法将 location 信息封装到 request 中然后转发到 selectList.jsp 页面)

  296. protected void getCity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  297.     LocationsDao locationsDao = new LocationsDaoImpl();  
  298.     List<Locations> locationsList = locationsDao.getAll();  
  299.     request.setAttribute("locations", locationsList);  
  300.     request.getRequestDispatcher("/selectList/selectList.jsp").forward(request, response);  
  301. }  

    selectList.jsp (将转发回页面的 locations 信息显示)

  302. <%--  
  303.   Created by IntelliJ IDEA.  
  304.   User: yin'zhao  
  305.   Date: 2017/12/04  
  306.   Time: 21:37  
  307.   To change this template use File | Settings | File Templates.  
  308. --%>  
  309. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  310. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  311. <html>  
  312. <head>  
  313.     <title>ShowInfoPage</title>  
  314. </head>  
  315. <body>  
  316. <spac>City:  </spac>  
  317. <select name="citySelect" id="city">  
  318.     <option value="choose">请选择</option>  
  319.     <c:forEach items="${requestScope.locations}" var="location">  
  320.         <option value="${location.locationId}">${location.city}</option>  
  321.     </c:forEach>  
  322. </select>  
  323. <span>        </span>  
  324. <span>Department:  </span>  
  325. <select name="departmentSelect" id="department">  
  326.     <option value="choose">请选择</option>  
  327. </select>  
  328. <span>        </span>  
  329. <span>Employee:  </span>  
  330. <select name="employeeSelect" id="employee">  
  331.     <option value="choose">请选择</option>  
  332. </select>  
  333. <table cellspacing="0" border="1" cellpadding="10" id="table">  
  334.     <tr>  
  335.         <th>employeeId</th>  
  336.         <th>lastName</th>  
  337.         <th>email</th>  
  338.     </tr>  
  339.     <tr>  
  340.         <td id="employeeId"></td>  
  341.         <td id="lastName"></td>  
  342.         <td id="email"></td>  
  343.     </tr>  
  344. </table>  
  345. </body>  
  346. </html>  

    b. 为下拉框添加 change 事件,每当 locations 改变后根据 location_id 获取departments 信息,并将其显示在对应的下拉框中(信息的显示利用 Ajax,若所选择 location 没有departments 则提示对应的消息)

    selectList.jsp (处理 Ajax 请求部分)

  347. <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-1.7.2.js"></script>  
  348.     <script type="text/javascript">  
  349.         $(function () {  
  350.             $("#city").change(function () {  
  351. //                每次选择都将除了第一个的所有选项移除即更新操作  
  352.                 $("#department option:not(:first)").remove();  
  353.                 var url = "${pageContext.request.contextPath}/getDepartment.do";  
  354. //                获取选中的 value 值,locationId  
  355.                 var val = $(this).val();  
  356. //                如果所选值不是第一个那么就开始执行 Ajax 操作,并将所选择的location 的 id 传到 servlet 中  
  357.                 if (val != "choose") {  
  358.                     var args = {"time": new Date, "value": val};  
  359.                     $.getJSON(url, args, function (data) {  
  360.                         if (data == 0) {  
  361. //                若传回的数据为空则提示错误消息  
  362.                             alert("当前城市没有部门");  
  363.                         } else {  
  364. //                否则将其加入对应的下拉框中  
  365.                             for (var i = 0; i < data.length; i++) {  
  366.                                 var departmentId = data[i].departmentId;  
  367.                                 var departmentName = data[i].departmentName;  
  368.                                 $("#department").append("<option value='" + departmentId + "'>" + departmentName + "</option>");  
  369.                             }  
  370.                         }  
  371.                     })  
  372.                 }  
  373.             })  
  374.             $("#department").change(function () {  
  375.                 $("#employee option:not(:first)").remove();  
  376.                 var url = "${pageContext.request.contextPath}/getEmployee.do";  
  377. //                获取选中的 value 值,departmentId  
  378.                 var val = $(this).val();  
  379.                 alert(val)  
  380.                 if (val != "choose") {  
  381.                     var args = {"time": new Date, "value": val};  
  382.                     $.getJSON(url, args, function (data) {  
  383.                         if (data == 0) {  
  384.                             alert("当前部门没有员工");  
  385.                         } else {  
  386.                             for (var i = 0; i < data.length; i++) {  
  387.                                 var employeeId = data[i].employeeId;  
  388.                                 var lastName = data[i].lastName;  
  389.                                 $("#employee").append("<option value='" + employeeId + "'>" + lastName + "</option>");  
  390.                             }  
  391.                         }  
  392.                     })  
  393.                 }  
  394.             })  
  395. </script>

    getDepartments(Servlet 方法,根据所选择的 location 信息获取对应的 departments 信息)

  396. protected void getDepartment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  397.     Integer locationId = Integer.valueOf(request.getParameter("value"));  
  398.     DepartmentsDao departmentsDao = new DepartmentsImpl();  
  399.     List<Departments> departmentsList = departmentsDao.getAll(locationId);  
  400.     ObjectMapper mapper = new ObjectMapper();  
  401.        将返回值转换为 json 格式  
  402.     String result = mapper.writeValueAsString(departmentsList);  
  403.     response.setContentType("text/javascript");  
  404.     response.getWriter().print(result);  
  405. }  

    getEmployees (Servlet 方法,根据所选择的 departments 信息获取对应的 employees 列表)

  406. protected void getEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  407.     Integer departmentId = Integer.valueOf(request.getParameter("value"));  
  408.     EmployeesDao departmentsDao = new EmployessDaoImpl();  
  409.     List<Employees> employeesList = departmentsDao.getAll(departmentId);  
  410.     ObjectMapper mapper = new ObjectMapper();  
  411.     String result = mapper.writeValueAsString(employeesList);  
  412.     response.setContentType("text/javascript");  
  413.     response.getWriter().print(result);  
  414. }  

    c. 选择对应的 employee 将其信息打印在页面(在这里我将完成的 jsp 页面和 servlet 代码粘贴在这里)

    selectList.jsp

  415. <%--  
  416.   Created by IntelliJ IDEA.  
  417.   User: yin'zhao  
  418.   Date: 2017/12/04  
  419.   Time: 21:37  
  420.   To change this template use File | Settings | File Templates.  
  421. --%>  
  422. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  423. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  424. <html>  
  425. <head>  
  426.     <title>ShowInfoPage</title>  
  427.     <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-1.7.2.js"></script>  
  428.     <script type="text/javascript">  
  429.         $(function () {  
  430.             /*  
  431.             * 刚开始将用于 employees 显示的表格隐藏  
  432.             * */  
  433.             $("#table").hide();  
  434.             $("#city").change(function () {  
  435.                 $("#department option:not(:first)").remove();  
  436.                 var url = "${pageContext.request.contextPath}/getDepartment.do";  
  437. //                获取选中的 value 值,locationId  
  438.                 var val = $(this).val();  
  439.                 if (val != "choose") {  
  440.                     var args = {"time": new Date, "value": val};  
  441.                     $.getJSON(url, args, function (data) {  
  442.                         if (data == 0) {  
  443.                             alert("当前城市没有部门");  
  444.                         } else {  
  445.                             for (var i = 0; i < data.length; i++) {  
  446.                                 var departmentId = data[i].departmentId;  
  447.                                 var departmentName = data[i].departmentName;  
  448.                                 $("#department").append("<option value='" + departmentId + "'>" + departmentName + "</option>");  
  449.                             }  
  450.                         }  
  451.                     })  
  452.                 }  
  453.             })  
  454.             $("#department").change(function () {  
  455.                 $("#employee option:not(:first)").remove();  
  456.                 var url = "${pageContext.request.contextPath}/getEmployee.do";  
  457. //                获取选中的 value 值,departmentId  
  458.                 var val = $(this).val();  
  459.                 alert(val)  
  460.                 if (val != "choose") {  
  461.                     var args = {"time": new Date, "value": val};  
  462.                     $.getJSON(url, args, function (data) {  
  463.                         if (data == 0) {  
  464.                             alert("当前部门没有员工");  
  465.                         } else {  
  466.                             for (var i = 0; i < data.length; i++) {  
  467.                                 var employeeId = data[i].employeeId;  
  468.                                 var lastName = data[i].lastName;  
  469.                                 $("#employee").append("<option value='" + employeeId + "'>" + lastName + "</option>");  
  470.                             }  
  471.                         }  
  472.                     })  
  473.                 }  
  474.             })  
  475.             $("#employee").change(function () {  
  476.                 var url = "${pageContext.request.contextPath}/showEmployee.do";  
  477. //                获取选中的 value 值,departmentId  
  478.                 var val = $(this).val();  
  479.                 if (val != "choose") {  
  480.                     var args = {"time": new Date, "value": val};  
  481.                     $.getJSON(url, args, function (data) {  
  482.                         var employeeId = data.employeeId;  
  483.                         var lastName = data.lastName;  
  484.                         var email = data.email;  
  485. //                        如果所选择的为 employees 而不是第一行,那么就将表格显示,并加入数据  
  486.                         $("#table").show();  
  487.                         $("#employeeId").text(employeeId);  
  488.                         $("#lastName").text(lastName);  
  489.                         $("#email").text(email);  
  490.                     })  
  491.                 } else {  
  492. //                    若选择的不是 employees 则将其隐藏  
  493.                     $("#table").hide();  
  494.                 }  
  495.             })  
  496.         })  
  497.     </script>  
  498. </head>  
  499. <body>  
  500. <spac>City:  </spac>  
  501. <select name="citySelect" id="city">  
  502.     <option value="choose">请选择</option>  
  503.     <c:forEach items="${requestScope.locations}" var="location">  
  504.         <option value="${location.locationId}">${location.city}</option>  
  505.     </c:forEach>  
  506. </select>  
  507. <span>        </span>  
  508. <span>Department:  </span>  
  509. <select name="departmentSelect" id="department">  
  510.     <option value="choose">请选择</option>  
  511. </select>  
  512. <span>        </span>  
  513. <span>Employee:  </span>  
  514. <select name="employeeSelect" id="employee">  
  515.     <option value="choose">请选择</option>  
  516. </select>  
  517. /*  
  518.   将 employees 的详细信息打印在此表格中  
  519. */  
  520. <table cellspacing="0" border="1" cellpadding="10" id="table">  
  521.     <tr>  
  522.         <th>employeeId</th>  
  523.         <th>lastName</th>  
  524.         <th>email</th>  
  525.     </tr>  
  526.     <tr>  
  527.         <td id="employeeId"></td>  
  528.         <td id="lastName"></td>  
  529.         <td id="email"></td>  
  530.     </tr>  
  531. </table>  
  532. </body>  
  533. </html>  

    ShowInfoServlet.java

  534. package com.javaweb.userajax.serlet.list.show.servlet;  
  535. import com.fasterxml.jackson.databind.ObjectMapper;  
  536. import com.javaweb.userajax.serlet.list.show.dao.DepartmentsDao;  
  537. import com.javaweb.userajax.serlet.list.show.dao.EmployeesDao;  
  538. import com.javaweb.userajax.serlet.list.show.dao.LocationsDao;  
  539. import com.javaweb.userajax.serlet.list.show.daoimpl.DepartmentsImpl;  
  540. import com.javaweb.userajax.serlet.list.show.daoimpl.EmployessDaoImpl;  
  541. import com.javaweb.userajax.serlet.list.show.daoimpl.LocationsDaoImpl;  
  542. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  543. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  544. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  545. import javax.servlet.ServletException;  
  546. import javax.servlet.http.HttpServlet;  
  547. import javax.servlet.http.HttpServletRequest;  
  548. import javax.servlet.http.HttpServletResponse;  
  549. import java.io.IOException;  
  550. import java.lang.reflect.InvocationTargetException;  
  551. import java.lang.reflect.Method;  
  552. import java.util.List;  
  553. /** 
  554.  * Created by shkstart on 2017/12/06. 
  555.  */  
  556. public class ShowInfoServlet extends HttpServlet {  
  557. //    获取请求的参数,利用反射执行当前类中对应的方法  
  558.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  559.         String servletPath = request.getServletPath();  
  560.         String methodName = servletPath.substring(1, servletPath.length() - 3);  
  561.         try {  
  562.             Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);  
  563.             method.invoke(this, request, response);  
  564.         } catch (NoSuchMethodException e) {  
  565.             e.printStackTrace();  
  566.         } catch (IllegalAccessException e) {  
  567.             e.printStackTrace();  
  568.         } catch (InvocationTargetException e) {  
  569.             e.printStackTrace();  
  570.         }  
  571.     }  
  572.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  573.         doPost(request, response);  
  574.     }  
  575.     protected void getCity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  576.         LocationsDao locationsDao = new LocationsDaoImpl();  
  577.         List<Locations> locationsList = locationsDao.getAll();  
  578.         request.setAttribute("locations", locationsList);  
  579.         request.getRequestDispatcher("/selectList/selectList.jsp").forward(request, response);  
  580.     }  
  581.     protected void getDepartment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  582.         Integer locationId = Integer.valueOf(request.getParameter("value"));  
  583.         DepartmentsDao departmentsDao = new DepartmentsImpl();  
  584.         List<Departments> departmentsList = departmentsDao.getAll(locationId);  
  585.         ObjectMapper mapper = new ObjectMapper();  
  586.         String result = mapper.writeValueAsString(departmentsList);  
  587.         response.setContentType("text/javascript");  
  588.         response.getWriter().print(result);  
  589.     }  
  590.     protected void getEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  591.         Integer departmentId = Integer.valueOf(request.getParameter("value"));  
  592.         EmployeesDao departmentsDao = new EmployessDaoImpl();  
  593.         List<Employees> employeesList = departmentsDao.getAll(departmentId);  
  594.         ObjectMapper mapper = new ObjectMapper();  
  595.         String result = mapper.writeValueAsString(employeesList);  
  596.         response.setContentType("text/javascript");  
  597.         response.getWriter().print(result);  
  598.     }  
  599.     protected void showEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  600.         Integer employeeId = Integer.valueOf(request.getParameter("value"));  
  601.         EmployeesDao employeesDao = new EmployessDaoImpl();  
  602.         Employees employees = employeesDao.getEmp(employeeId);  
  603.         ObjectMapper mapper = new ObjectMapper();  
  604.         String result = mapper.writeValueAsString(employees);  
  605.         System.out.println(result);  
  606.         response.setContentType("text/javascript");  
  607.         response.getWriter().print(result);  
  608.     }  
  609. }  

    以上就是我这次博文的内容,如果哪位读者发现错误以及表述不正确的地方还望指出,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值