问题描述
- servlet +ajax 关于 典型应用_三级联动__城市到部门的时 出错了,谁能帮我下
-
servlet +ajax 关于发送$.getJson EmployeeServlet 无法得到,谁能帮我解决下
在学习 51CTO下载-11. 尚硅谷_佟刚_Ajax_典型应用_三级联动__城市到部门的 Ajax 实现
如图
有那个高手帮我解决下。在这先谢过了
解决方案
解决方案三:
以下是代码**
**
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
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.fasterxml.jackson.databind.ObjectMapper;
import beans.Department;
import beans.Location;
import dao.BaseDao;
public class EmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private BaseDao baseDao = new BaseDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String methodName = request.getParameter("method");
try{
Method method = getClass().getDeclaredMethod(methodName,HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request,response);
}catch(Exception e){
e.printStackTrace();
}
}
public void listLocations(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sql = "SELECT location_id locationId, city FROM locations";
List<Location> locations = baseDao.getForList(sql, Location.class);
request.setAttribute("locations", locations);
System.out.println("locations="+locations);
request.getRequestDispatcher("/WEB-INF/pages/employees.jsp").forward(request, response);
}
public void listDepartments(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String locationId = request.getParameter("locationId");
String sql = "SELECT departmet_id departmentId, department_name departmentName"+
"FROM departments d"+
"WHERE d.location_id=?";
List<Department> departments = baseDao.getForList(sql, Department.class, Integer.parseInt(locationId));
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(departments);
System.out.println(result);
response.setContentType("text/javascript");
response.getWriter().print(result);
}
}
解决方案五:
自己顶一下贴子**__**
解决方案六:
自己顶一下贴子**__**
解决方案七:
自己顶一下贴子**__**
解决方案八:
自己顶一下贴子**__**
时间: 2025-01-30 13:05:27