package cn.emp.controller;
import cn.emp.entity.Employee;
import cn.emp.service.IEmployeeService;
import cn.emp.service.impl.EmployeeServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/emp/*")
public class EmployeeController extends HttpServlet {
private IEmployeeService employeeService = new EmployeeServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uri = req.getRequestURI();
//System.out.println("uri:"+uri);
if (uri.equals("/emp/list")){ //列表查询
list(req,resp);
}else if(uri.equals("/emp/delete")){ //删除员工
delete(req,resp);
}else if(uri.equals("/emp/showAdd")){ //跳转新增页面
req.getRequestDispatcher("/WEB-INF/views/addEmp.jsp").forward(req,resp);
}else if(uri.equals("/emp/add")){ //新增员工
add(req,resp);
}else if(uri.equals("/emp/showUpdate")){ //跳转修改页面
showUpdate(req,resp);
}else if(uri.equals("/emp/update")){ //修改员工
update(req,resp);
}
}
//修改员工
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("UTF-8");
//2.获取参数
Long id = Long.valueOf(req.getParameter("id"));
String name = req.getParameter("name");
Integer age = Integer.valueOf(req.getParameter("age"));
Integer sex = Integer.valueOf(req.getParameter("sex"));
String phone = req.getParameter("phone");
String address = req.getParameter("address");
Employee employee = new Employee();
employee.setId(id);
employee.setName(name);
employee.setAge(age);
employee.setSex(sex);
employee.setPhone(phone);
employee.setAddress(address);
//3.调用业务层执行修改
employeeService.update(employee);
//4.重定向到列表页
resp.sendRedirect("/emp/list");
}
//跳转修改页面
protected void showUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取id
Long id = Long.valueOf(req.getParameter("id"));
//2.基于id查询员工信息
Employee employee = employeeService.selectById(id);
//3.将员工信息存入request作用域中
req.setAttribute("employee",employee);
//4.转发到修改页面,并展示员工信息
req.getRequestDispatcher("/WEB-INF/views/updateEmp.jsp").forward(req,resp);
}
//新增员工
protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("UTF-8");
//2.获取参数
String name = req.getParameter("name");
Integer age = Integer.valueOf(req.getParameter("age"));
Integer sex = Integer.valueOf(req.getParameter("sex"));
String phone = req.getParameter("phone");
String address = req.getParameter("address");
Employee employee = new Employee();
employee.setName(name);
employee.setAge(age);
employee.setSex(sex);
employee.setPhone(phone);
employee.setAddress(address);
//3.调用业务层执行新增
employeeService.insert(employee);
//4.重定向到列表页
resp.sendRedirect("/emp/list");
}
//删除员工
protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取id
String id = req.getParameter("id");
//2.调用业务层执行删除
employeeService.delete(Long.valueOf(id));
//3.重定向到列表页
resp.sendRedirect("/emp/list");
}
//列表查询
protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.调用业务层获取员工列表数据
List<Employee> employees = employeeService.selectAll();
//2.封装到request作用域中
req.setAttribute("employees",employees);
//3.转发到列表页面,展示数据
req.getRequestDispatcher("/WEB-INF/views/empList.jsp").forward(req,resp);
}
}
评论0