|
@@ -0,0 +1,117 @@
|
|
|
+package controller;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URI;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+
|
|
|
+import model.Employee;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.HttpRequest;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.ui.Model;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+
|
|
|
+import service.EmployeeService;
|
|
|
+
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/employee")
|
|
|
+public class EmployeeController {
|
|
|
+ @Autowired
|
|
|
+ private EmployeeService employeeService;
|
|
|
+ @RequestMapping("/selectEmployees")
|
|
|
+ public String selectEmployees(HttpSession session,@RequestParam(required=false,defaultValue="1")int pageIndex,
|
|
|
+ @RequestParam(required=false,defaultValue="5")int pageSize){
|
|
|
+ PageInfo<Employee> employeeList=employeeService.selectEmployees(pageIndex,pageSize);
|
|
|
+ session.setAttribute("employees", employeeList);
|
|
|
+ return "employee/employees";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/toInsertForm")
|
|
|
+ public String toInsertForm(){
|
|
|
+ return "employee/insertForm";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/insertEmployee")
|
|
|
+ public String insertEmployee(Employee employee,HttpSession session){
|
|
|
+
|
|
|
+ int rows=employeeService.insertEmployee(employee);
|
|
|
+ return "forward:/employee/selectEmployees";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/toUpdateForm")
|
|
|
+ public String toUpdateForm(int id,HttpSession session,Model model){
|
|
|
+ Employee employee=null;
|
|
|
+ PageInfo<Employee> employeeList=(PageInfo<Employee>) session.getAttribute("employees");
|
|
|
+ if(employeeList!=null){
|
|
|
+ List<Employee> list=employeeList.getList();
|
|
|
+ for(Employee a:list){
|
|
|
+ if(a.getId()==id){
|
|
|
+ employee=a;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ model.addAttribute("employee", employee);
|
|
|
+ return "employee/updateForm";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/updateEmployee")
|
|
|
+ public String updateEmployee(Employee employee,HttpSession session,HttpServletRequest request) throws UnsupportedEncodingException{
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+
|
|
|
+ int rows=employeeService.updateEmployee(employee);
|
|
|
+ if(rows>0){
|
|
|
+ PageInfo<Employee> employeeList=(PageInfo<Employee>) session.getAttribute("employees");
|
|
|
+ List<Employee> list=employeeList.getList();
|
|
|
+ for(Employee a:list){
|
|
|
+ if(a.getId()==employee.getId()){
|
|
|
+ a.setName(employee.getName());
|
|
|
+ a.setAge(employee.getAge());
|
|
|
+ a.setSalary(employee.getSalary());
|
|
|
+ a.setDepartment(employee.getDepartment());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ session.setAttribute("employees",employeeList);
|
|
|
+ }
|
|
|
+ return "employee/employees";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/deleteEmployee")
|
|
|
+ public String deleteEmployee(int id,HttpSession session){
|
|
|
+
|
|
|
+ int rows=employeeService.deleteEmployee(id);
|
|
|
+
|
|
|
+ if(rows>0){
|
|
|
+ PageInfo<Employee> employeeList=(PageInfo<Employee>) session.getAttribute("employees");
|
|
|
+ List<Employee> list=employeeList.getList();
|
|
|
+ for(Employee a:list){
|
|
|
+ if(a.getId()==id){
|
|
|
+ list.remove(a);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ session.setAttribute("employees", employeeList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return "employee/employees";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|