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.     
  3. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  4.     
  5. import javax.sql.DataSource;  
  6. import java.sql.Connection;  
  7. import java.sql.SQLException;  
  8.     
  9. /** 
  10.  * Created by shkstart on 2017/12/05. 
  11.  */  
  12. public class JDBCTools {  
  13.     
  14.     private static DataSource dataSource;  
  15.     static {  
  16.         dataSource = new ComboPooledDataSource("listShow");  
  17.     }  
  18.     
  19.     public static Connection getConnection() {  
  20.         Connection connection = null;  
  21.         try {  
  22.             connection = dataSource.getConnection();  
  23.         } catch (SQLException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return connection;  
  27.     }  
  28.     
  29.     public void releaseConnection(Connection connection) {  
  30.         try {  
  31.             connection.close();  
  32.         } catch (SQLException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.     }  
  36. }  

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

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

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

    DepartmentsDao:

  106. package com.javaweb.userajax.serlet.list.show.dao;  
  107.     
  108. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  109.     
  110. import java.util.List;  
  111.     
  112. /** 
  113.  * Created by shkstart on 2017/12/06. 
  114.  */  
  115. public interface DepartmentsDao {  
  116.     List<Departments> getAll(Integer locationId);  
  117. }  

    EmployeesDao:

  118. package com.javaweb.userajax.serlet.list.show.dao;  
  119.     
  120. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  121.     
  122. import java.util.List;  
  123.     
  124. /** 
  125.  * Created by shkstart on 2017/12/06. 
  126.  */  
  127. public interface EmployeesDao {  
  128.     List<Employees> getAll(Integer departmentId);  
  129.     Employees getEmp(Integer employeeId);  
  130. }  

    LocationsDao:

  131. package com.javaweb.userajax.serlet.list.show.dao;  
  132.     
  133. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  134.     
  135. import java.util.List;  
  136.     
  137. /** 
  138.  * Created by shkstart on 2017/12/06. 
  139.  */  
  140. public interface LocationsDao {  
  141.     List<Locations> getAll();  
  142. }  

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

    Departments:

  143. package com.javaweb.userajax.serlet.list.show.domain;  
  144.     
  145. /** 
  146.  * Created by shkstart on 2017/12/06. 
  147.  */  
  148. public class Departments {  
  149.     private Integer departmentId;  
  150.     private String departmentName;  
  151.     private Integer locationId;  
  152.     
  153.     public Integer getDepartmentId() {  
  154.         return departmentId;  
  155.     }  
  156.     
  157.     public void setDepartmentId(Integer departmentId) {  
  158.         this.departmentId = departmentId;  
  159.     }  
  160.     
  161.     public String getDepartmentName() {  
  162.         return departmentName;  
  163.     }  
  164.     
  165.     public void setDepartmentName(String departmentName) {  
  166.         this.departmentName = departmentName;  
  167.     }  
  168.     
  169.     public Integer getLocationId() {  
  170.         return locationId;  
  171.     }  
  172.     
  173.     public void setLocationId(Integer locationId) {  
  174.         this.locationId = locationId;  
  175.     }  
  176.     
  177.     public Departments(){  
  178.     
  179.     }  
  180.     
  181.     @Override  
  182.     public String toString() {  
  183.         return "Departments{" +  
  184.                 "departmentId=" + departmentId +  
  185.                 ", departmentName='" + departmentName + '\'' +  
  186.                 ", locationId=" + locationId +  
  187.                 '}';  
  188.     }  
  189. }  

    Employees:

  190. package com.javaweb.userajax.serlet.list.show.domain;  
  191.     
  192. /** 
  193.  * Created by shkstart on 2017/12/06. 
  194.  */  
  195. public class Employees {  
  196.     private Integer employeeId;  
  197.     private Integer departmentId;  
  198.     private String lastName;  
  199.     private String email;  
  200.     
  201.     @Override  
  202.     public String toString() {  
  203.         return "Employees{" +  
  204.                 "employeeId=" + employeeId +  
  205.                 ", departmentId=" + departmentId +  
  206.                 ", lastName='" + lastName + '\'' +  
  207.                 ", email='" + email + '\'' +  
  208.                 '}';  
  209.     }  
  210.     
  211.     public Integer getDepartmentId() {  
  212.         return departmentId;  
  213.     }  
  214.     
  215.     public void setDepartmentId(Integer departmentId) {  
  216.         this.departmentId = departmentId;  
  217.     }  
  218.     
  219.     public Integer getEmployeeId() {  
  220.         return employeeId;  
  221.     }  
  222.     
  223.     public void setEmployeeId(Integer employeeId) {  
  224.         this.employeeId = employeeId;  
  225.     }  
  226.     
  227.     public String getLastName() {  
  228.         return lastName;  
  229.     }  
  230.     
  231.     public void setLastName(String lastName) {  
  232.         this.lastName = lastName;  
  233.     }  
  234.     
  235.     public String getEmail() {  
  236.         return email;  
  237.     }  
  238.     
  239.     public void setEmail(String email) {  
  240.         this.email = email;  
  241.     }  
  242.     
  243.     public Employees(){  
  244.     
  245.     }  
  246.     
  247. }  

    Locations:

  248. package com.javaweb.userajax.serlet.list.show.domain;  
  249.     
  250. /** 
  251.  * Created by shkstart on 2017/12/06. 
  252.  */  
  253. public class Locations {  
  254.     private Integer locationId;  
  255.     private String city;  
  256.     
  257.     public Integer getLocationId() {  
  258.         return locationId;  
  259.     }  
  260.     
  261.     public void setLocationId(Integer locationId) {  
  262.         this.locationId = locationId;  
  263.     }  
  264.     
  265.     public String getCity() {  
  266.         return city;  
  267.     }  
  268.     
  269.     public void setCity(String city) {  
  270.         this.city = city;  
  271.     }  
  272.     
  273.     public Locations() {  
  274.     
  275.     }  
  276.     
  277.     @Override  
  278.     public String toString() {  
  279.         return "Locations{" +  
  280.                 "locationId=" + locationId +  
  281.                 ", city='" + city + '\'' +  
  282.                 '}';  
  283.     }  
  284. }  

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

    DepartmentsImpl:

  285. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  286.     
  287. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  288. import com.javaweb.userajax.serlet.list.show.dao.DepartmentsDao;  
  289. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  290.     
  291. import java.util.List;  
  292.     
  293. /** 
  294.  * Created by shkstart on 2017/12/06. 
  295.  */  
  296. public class DepartmentsImpl extends DAO<Departments> implements DepartmentsDao {  
  297.     @Override  
  298.     public List<Departments> getAll(Integer locationId) {  
  299.         String sql = "SELECT department_id departmentId, department_name departmentName, location_id locationId FROM departments WHERE location_id=?";  
  300.         System.out.println(sql);  
  301.         List<Departments> departmentsList = getList(sql, locationId);  
  302.         return departmentsList;  
  303.     }  
  304. }  

    EmployeesImpl:

  305. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  306.     
  307. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  308. import com.javaweb.userajax.serlet.list.show.dao.EmployeesDao;  
  309. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  310.     
  311. import java.util.List;  
  312.     
  313. /** 
  314.  * Created by shkstart on 2017/12/06. 
  315.  */  
  316. public class EmployessDaoImpl extends DAO<Employees> implements EmployeesDao {  
  317.     @Override  
  318.     public List<Employees> getAll(Integer departmentId) {  
  319.         String sql = "SELECT employee_id employeeId, last_name lastName FROM employees WHERE department_id=?";  
  320.         System.out.println(sql);  
  321.         List<Employees> employeesList = getList(sql, departmentId);  
  322.         return employeesList;  
  323.     }  
  324.     
  325.     @Override  
  326.     public Employees getEmp(Integer employeeId) {  
  327.         String sql = "SELECT employee_id employeeId, department_id departmentId, last_name lastName, email FROM employees WHERE employee_id=?";  
  328.         Employees employees = getValue(sql, employeeId);  
  329.         return employees;  
  330.     }  
  331. }  

    LocationsImpl:

  332. package com.javaweb.userajax.serlet.list.show.daoimpl;  
  333.     
  334. import com.javaweb.userajax.serlet.list.show.dao.DAO;  
  335. import com.javaweb.userajax.serlet.list.show.dao.LocationsDao;  
  336. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  337.     
  338. import java.util.List;  
  339.     
  340. /** 
  341.  * Created by shkstart on 2017/12/06. 
  342.  */  
  343. public class LocationsDaoImpl extends DAO<Locations> implements LocationsDao {  
  344.     
  345.     @Override  
  346.     public List<Locations> getAll() {  
  347.         String sql = "SELECT location_id locationId, city FROM locations";  
  348.     
  349.         List<Locations> locationsList = getAll(sql);  
  350.         return locationsList;  
  351.     }  
  352. }  
    1. JSP 页面实现

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

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

  353. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  354. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  355. <html>  
  356. <head>  
  357.     <title>ForwardPage</title>  
  358. <body>  
  359. <h3></h3>  
  360. <jsp:forward page="getCity.do"/>  
  361. </body>  
  362. </html>  

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

  363. protected void getCity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  364.     LocationsDao locationsDao = new LocationsDaoImpl();  
  365.     List<Locations> locationsList = locationsDao.getAll();  
  366.     request.setAttribute("locations", locationsList);  
  367.     request.getRequestDispatcher("/selectList/selectList.jsp").forward(request, response);  
  368. }  

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

  369. <%--  
  370.   Created by IntelliJ IDEA.  
  371.   User: yin'zhao  
  372.   Date: 2017/12/04  
  373.   Time: 21:37  
  374.   To change this template use File | Settings | File Templates.  
  375. --%>  
  376. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  377. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  378. <html>  
  379. <head>  
  380.     <title>ShowInfoPage</title>  
  381. </head>  
  382. <body>  
  383. <spac>City:  </spac>  
  384. <select name="citySelect" id="city">  
  385.     <option value="choose">请选择</option>  
  386.     <c:forEach items="${requestScope.locations}" var="location">  
  387.         <option value="${location.locationId}">${location.city}</option>  
  388.     </c:forEach>  
  389. </select>  
  390. <span>        </span>  
  391. <span>Department:  </span>  
  392.     
  393. <select name="departmentSelect" id="department">  
  394.     <option value="choose">请选择</option>  
  395. </select>  
  396. <span>        </span>  
  397. <span>Employee:  </span>  
  398.     
  399. <select name="employeeSelect" id="employee">  
  400.     <option value="choose">请选择</option>  
  401. </select>  
  402. <table cellspacing="0" border="1" cellpadding="10" id="table">  
  403.     <tr>  
  404.         <th>employeeId</th>  
  405.         <th>lastName</th>  
  406.         <th>email</th>  
  407.     </tr>  
  408.     <tr>  
  409.         <td id="employeeId"></td>  
  410.         <td id="lastName"></td>  
  411.         <td id="email"></td>  
  412.     </tr>  
  413. </table>  
  414. </body>  
  415. </html>  

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

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

  416. <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-1.7.2.js"></script>  
  417.     <script type="text/javascript">  
  418.         $(function () {  
  419.             $("#city").change(function () {  
  420. //                每次选择都将除了第一个的所有选项移除即更新操作  
  421.                 $("#department option:not(:first)").remove();  
  422.                 var url = "${pageContext.request.contextPath}/getDepartment.do";  
  423. //                获取选中的 value 值,locationId  
  424.                 var val = $(this).val();  
  425. //                如果所选值不是第一个那么就开始执行 Ajax 操作,并将所选择的location id 传到 servlet   
  426.                 if (val != "choose") {  
  427.                     var args = {"time": new Date, "value": val};  
  428.                     $.getJSON(url, args, function (data) {  
  429.                         if (data == 0) {  
  430. //                若传回的数据为空则提示错误消息  
  431.                             alert("当前城市没有部门");  
  432.                         } else {  
  433. //                否则将其加入对应的下拉框中  
  434.                             for (var i = 0; i < data.length; i++) {  
  435.                                 var departmentId = data[i].departmentId;  
  436.                                 var departmentName = data[i].departmentName;  
  437.                                 $("#department").append("<option value='" + departmentId + "'>" + departmentName + "</option>");  
  438.                             }  
  439.                         }  
  440.                     })  
  441.                 }  
  442.             })  
  443.     
  444.             $("#department").change(function () {  
  445.                 $("#employee option:not(:first)").remove();  
  446.                 var url = "${pageContext.request.contextPath}/getEmployee.do";  
  447. //                获取选中的 value 值,departmentId  
  448.                 var val = $(this).val();  
  449.                 alert(val)  
  450.                 if (val != "choose") {  
  451.                     var args = {"time": new Date, "value": val};  
  452.                     $.getJSON(url, args, function (data) {  
  453.                         if (data == 0) {  
  454.                             alert("当前部门没有员工");  
  455.                         } else {  
  456.                             for (var i = 0; i < data.length; i++) {  
  457.                                 var employeeId = data[i].employeeId;  
  458.                                 var lastName = data[i].lastName;  
  459.                                 $("#employee").append("<option value='" + employeeId + "'>" + lastName + "</option>");  
  460.                             }  
  461.                         }  
  462.                     })  
  463.                 }  
  464.             })  
  465. </script>

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

  466. protected void getDepartment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  467.     Integer locationId = Integer.valueOf(request.getParameter("value"));  
  468.     DepartmentsDao departmentsDao = new DepartmentsImpl();  
  469.     
  470.     List<Departments> departmentsList = departmentsDao.getAll(locationId);  
  471.     ObjectMapper mapper = new ObjectMapper();  
  472.        将返回值转换为 json 格式  
  473.     String result = mapper.writeValueAsString(departmentsList);  
  474.     
  475.     response.setContentType("text/javascript");  
  476.     response.getWriter().print(result);  
  477. }  

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

  478. protected void getEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  479.     Integer departmentId = Integer.valueOf(request.getParameter("value"));  
  480.     EmployeesDao departmentsDao = new EmployessDaoImpl();  
  481.     
  482.     List<Employees> employeesList = departmentsDao.getAll(departmentId);  
  483.     ObjectMapper mapper = new ObjectMapper();  
  484.     String result = mapper.writeValueAsString(employeesList);  
  485.     
  486.     response.setContentType("text/javascript");  
  487.     response.getWriter().print(result);  
  488. }  

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

    selectList.jsp

  489. <%--  
  490.   Created by IntelliJ IDEA.  
  491.   User: yin'zhao  
  492.   Date: 2017/12/04  
  493.   Time: 21:37  
  494.   To change this template use File | Settings | File Templates.  
  495. --%>  
  496. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  497. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  498. <html>  
  499. <head>  
  500.     <title>ShowInfoPage</title>  
  501.     <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-1.7.2.js"></script>  
  502.     <script type="text/javascript">  
  503.         $(function () {  
  504.             /*  
  505.             * 刚开始将用于 employees 显示的表格隐藏  
  506.             * */  
  507.             $("#table").hide();  
  508.             $("#city").change(function () {  
  509.                 $("#department option:not(:first)").remove();  
  510.                 var url = "${pageContext.request.contextPath}/getDepartment.do";  
  511. //                获取选中的 value 值,locationId  
  512.                 var val = $(this).val();  
  513.                 if (val != "choose") {  
  514.                     var args = {"time": new Date, "value": val};  
  515.                     $.getJSON(url, args, function (data) {  
  516.                         if (data == 0) {  
  517.                             alert("当前城市没有部门");  
  518.                         } else {  
  519.                             for (var i = 0; i < data.length; i++) {  
  520.                                 var departmentId = data[i].departmentId;  
  521.                                 var departmentName = data[i].departmentName;  
  522.                                 $("#department").append("<option value='" + departmentId + "'>" + departmentName + "</option>");  
  523.                             }  
  524.                         }  
  525.                     })  
  526.                 }  
  527.             })  
  528.     
  529.             $("#department").change(function () {  
  530.                 $("#employee option:not(:first)").remove();  
  531.                 var url = "${pageContext.request.contextPath}/getEmployee.do";  
  532. //                获取选中的 value 值,departmentId  
  533.                 var val = $(this).val();  
  534.                 alert(val)  
  535.                 if (val != "choose") {  
  536.                     var args = {"time": new Date, "value": val};  
  537.                     $.getJSON(url, args, function (data) {  
  538.                         if (data == 0) {  
  539.                             alert("当前部门没有员工");  
  540.                         } else {  
  541.                             for (var i = 0; i < data.length; i++) {  
  542.                                 var employeeId = data[i].employeeId;  
  543.                                 var lastName = data[i].lastName;  
  544.                                 $("#employee").append("<option value='" + employeeId + "'>" + lastName + "</option>");  
  545.                             }  
  546.                         }  
  547.                     })  
  548.                 }  
  549.             })  
  550.     
  551.             $("#employee").change(function () {  
  552.                 var url = "${pageContext.request.contextPath}/showEmployee.do";  
  553. //                获取选中的 value 值,departmentId  
  554.                 var val = $(this).val();  
  555.                 if (val != "choose") {  
  556.                     var args = {"time": new Date, "value": val};  
  557.                     $.getJSON(url, args, function (data) {  
  558.                         var employeeId = data.employeeId;  
  559.                         var lastName = data.lastName;  
  560.                         var email = data.email;  
  561. //                        如果所选择的为 employees 而不是第一行,那么就将表格显示,并加入数据  
  562.                         $("#table").show();  
  563.                         $("#employeeId").text(employeeId);  
  564.                         $("#lastName").text(lastName);  
  565.                         $("#email").text(email);  
  566.                     })  
  567.                 } else {  
  568. //                    若选择的不是 employees 则将其隐藏  
  569.                     $("#table").hide();  
  570.                 }  
  571.             })  
  572.         })  
  573.     </script>  
  574. </head>  
  575. <body>  
  576. <spac>City:  </spac>  
  577. <select name="citySelect" id="city">  
  578.     <option value="choose">请选择</option>  
  579.     <c:forEach items="${requestScope.locations}" var="location">  
  580.         <option value="${location.locationId}">${location.city}</option>  
  581.     </c:forEach>  
  582. </select>  
  583. <span>        </span>  
  584. <span>Department:  </span>  
  585.     
  586. <select name="departmentSelect" id="department">  
  587.     <option value="choose">请选择</option>  
  588. </select>  
  589. <span>        </span>  
  590. <span>Employee:  </span>  
  591.     
  592. <select name="employeeSelect" id="employee">  
  593.     <option value="choose">请选择</option>  
  594. </select>  
  595. /*  
  596.    employees 的详细信息打印在此表格中  
  597. */  
  598. <table cellspacing="0" border="1" cellpadding="10" id="table">  
  599.     <tr>  
  600.         <th>employeeId</th>  
  601.         <th>lastName</th>  
  602.         <th>email</th>  
  603.     </tr>  
  604.     <tr>  
  605.         <td id="employeeId"></td>  
  606.         <td id="lastName"></td>  
  607.         <td id="email"></td>  
  608.     </tr>  
  609. </table>  
  610. </body>  
  611. </html>  

     

    ShowInfoServlet.java

  612. package com.javaweb.userajax.serlet.list.show.servlet;  
  613.     
  614. import com.fasterxml.jackson.databind.ObjectMapper;  
  615. import com.javaweb.userajax.serlet.list.show.dao.DepartmentsDao;  
  616. import com.javaweb.userajax.serlet.list.show.dao.EmployeesDao;  
  617. import com.javaweb.userajax.serlet.list.show.dao.LocationsDao;  
  618. import com.javaweb.userajax.serlet.list.show.daoimpl.DepartmentsImpl;  
  619. import com.javaweb.userajax.serlet.list.show.daoimpl.EmployessDaoImpl;  
  620. import com.javaweb.userajax.serlet.list.show.daoimpl.LocationsDaoImpl;  
  621. import com.javaweb.userajax.serlet.list.show.domain.Departments;  
  622. import com.javaweb.userajax.serlet.list.show.domain.Employees;  
  623. import com.javaweb.userajax.serlet.list.show.domain.Locations;  
  624.     
  625. import javax.servlet.ServletException;  
  626. import javax.servlet.http.HttpServlet;  
  627. import javax.servlet.http.HttpServletRequest;  
  628. import javax.servlet.http.HttpServletResponse;  
  629. import java.io.IOException;  
  630. import java.lang.reflect.InvocationTargetException;  
  631. import java.lang.reflect.Method;  
  632. import java.util.List;  
  633.     
  634. /** 
  635.  * Created by shkstart on 2017/12/06. 
  636.  */  
  637. public class ShowInfoServlet extends HttpServlet {  
  638. //    获取请求的参数,利用反射执行当前类中对应的方法  
  639.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  640.         String servletPath = request.getServletPath();  
  641.         String methodName = servletPath.substring(1, servletPath.length() - 3);  
  642.         try {  
  643.             Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);  
  644.             method.invoke(this, request, response);  
  645.         } catch (NoSuchMethodException e) {  
  646.             e.printStackTrace();  
  647.         } catch (IllegalAccessException e) {  
  648.             e.printStackTrace();  
  649.         } catch (InvocationTargetException e) {  
  650.             e.printStackTrace();  
  651.         }  
  652.     }  
  653.     
  654.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  655.         doPost(request, response);  
  656.     }  
  657.     protected void getCity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  658.         LocationsDao locationsDao = new LocationsDaoImpl();  
  659.         List<Locations> locationsList = locationsDao.getAll();  
  660.         request.setAttribute("locations", locationsList);  
  661.         request.getRequestDispatcher("/selectList/selectList.jsp").forward(request, response);  
  662.     }  
  663.     protected void getDepartment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  664.         Integer locationId = Integer.valueOf(request.getParameter("value"));  
  665.         DepartmentsDao departmentsDao = new DepartmentsImpl();  
  666.     
  667.         List<Departments> departmentsList = departmentsDao.getAll(locationId);  
  668.         ObjectMapper mapper = new ObjectMapper();  
  669.         String result = mapper.writeValueAsString(departmentsList);  
  670.     
  671.         response.setContentType("text/javascript");  
  672.         response.getWriter().print(result);  
  673.     }  
  674.     protected void getEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  675.         Integer departmentId = Integer.valueOf(request.getParameter("value"));  
  676.         EmployeesDao departmentsDao = new EmployessDaoImpl();  
  677.     
  678.         List<Employees> employeesList = departmentsDao.getAll(departmentId);  
  679.         ObjectMapper mapper = new ObjectMapper();  
  680.         String result = mapper.writeValueAsString(employeesList);  
  681.     
  682.         response.setContentType("text/javascript");  
  683.         response.getWriter().print(result);  
  684.     }  
  685.     
  686.     protected void showEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  687.         Integer employeeId = Integer.valueOf(request.getParameter("value"));  
  688.         EmployeesDao employeesDao = new EmployessDaoImpl();  
  689.         Employees employees = employeesDao.getEmp(employeeId);  
  690.     
  691.         ObjectMapper mapper = new ObjectMapper();  
  692.         String result = mapper.writeValueAsString(employees);  
  693.         System.out.println(result);  
  694.     
  695.         response.setContentType("text/javascript");  
  696.         response.getWriter().print(result);  
  697.     }  
  698. }  

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

posted @ 2018-01-02 11:27  bgzyy  阅读(449)  评论(2编辑  收藏  举报