controller层 #1

Merged
p6jzelshw merged 4 commits from py2tc4x5b/ssm:master into master 2 years ago

@ -0,0 +1,130 @@
package com.controller;
import com.entity.Department;
import com.service.IDeptService;
import com.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName DepartmentController
* @Description TODO
* @Author YHT
* @Date 2021/5/30 12:14
*/
@Controller
public class DepartmentController {
@Autowired
private IDeptService deptService;
/***
*
* @param page
* @return
*/
@RequestMapping("/toDepartment")
public ModelAndView toDepartment(Page page){
ModelAndView mv = new ModelAndView();
List<Department> departmentList = deptService.listDept(page);
int total = deptService.getDeptTotal();
int pageIndex = total / page.getCount();
pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1;
page.setTotalIndex(pageIndex);
page.calculateLast(total);
mv.addObject("deptList",departmentList);
mv.addObject("page",page);
mv.addObject("total",total);
mv.setViewName("department");
return mv;
}
/**
*
* @param department
* @return
*/
@RequestMapping("/addDept")
public ModelAndView addDept(Department department){
ModelAndView mv = new ModelAndView();
//调用service层的方法
deptService.addDept(department);
//重定向到部门列表页面
mv.setViewName("redirect:/toDepartment");
return mv;
}
/**
*
* @param deptName
* @param page
* @return
*/
@RequestMapping("/searchDept")
public ModelAndView searchDept(String deptName, Page page){
ModelAndView mv = new ModelAndView();
//根据部门名称查找部门信息,并进行分页显示
List<Department> departmentList = deptService.findDeptByName(deptName, page);
//得到总记录数
int total = departmentList.size();
//每页显示三条,总共显示的页数
int pageIndex = total / page.getCount();
pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1;
page.setTotalIndex(pageIndex);
page.calculateLast(total);
//添加信息到modelview。
mv.addObject("deptList",departmentList);
mv.addObject("page",page);
mv.addObject("total",total);
//返回部门信息列表页面
mv.setViewName("department");
return mv;
}
/**
*
* @param id id
* @return
*/
@RequestMapping("/toModifyDept")
public ModelAndView toModifyDept(int id){
ModelAndView mv = new ModelAndView();
//找到部门信息
Department department = deptService.findDeptById(id);
//将该部门信息返回到页面
mv.addObject("dept",department);
//跳转至修改信息界面
mv.setViewName("updateDepartment");
return mv;
}
/**
*
* @param department
* @return
*/
@RequestMapping("/modifyDept")
public ModelAndView modifyDept(Department department){
ModelAndView mv = new ModelAndView();
//修改部门信息
deptService.modifyDept(department);
//重定向到部门信息列表界面
mv.setViewName("redirect:/toDepartment");
return mv;
}
@RequestMapping("/deleteDept")
public ModelAndView deleteDept(int id){
ModelAndView mv = new ModelAndView();
//修改部门信息
deptService.deleteDept(id);
//重定向到部门信息列表界面
mv.setViewName("redirect:/toDepartment");
return mv;
}
}

@ -0,0 +1,181 @@
package com.controller;
import com.entity.Department;
import com.entity.Employee;
import com.service.IDeptService;
import com.service.IEmpService;
import com.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName EmployeeController
* @Description TODO
* @Author YHT
* @Date 2021/5/30 12:15
*/
@Controller
public class EmployeeController {
@Autowired
private IEmpService empService;
@Autowired
private IDeptService deptService;
/**
*
* @param page
* @return
*/
@RequestMapping("/toEmployee")
public ModelAndView toEmployee(Page page){
ModelAndView mv = new ModelAndView();
//查询所有员工
List<Employee> employeeList = empService.listEmp(page);
//给每个员工设置部门名称
for (Employee emp : employeeList) {
emp.setDeptName(deptService.findDeptById(emp.getDeptId()).getDeptName());
}
//获取数据库中记录总数
int total = empService.getEmpTotal();
//页数。即:每页显示三条记录,总共展示几页
int pageIndex = total / page.getCount();
pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1;
page.setTotalIndex(pageIndex);
page.calculateLast(total);
//将数据返回员工列表页面
mv.addObject("empList",employeeList);
mv.addObject("page",page);
mv.addObject("total",total);
mv.setViewName("employee");
return mv;
}
/**
*
* @param page
* @param name
* @param email
* @param empId
* @return
*/
@RequestMapping("/searchEmp")
public ModelAndView toEmployee(Page page, String name, String email, String empId){
ModelAndView mv = new ModelAndView();
//根据部门名称查找部门信息,并进行分页显示
Map<String, Object> map = new HashMap<>();
if(name != "" && name != null){
map.put("name", name);
}
if(email != "" && email != null){
map.put("email", email);
}
if(empId != "" && empId != null){
map.put("empId", empId);
}
map.put("page", page);
//得到符合条件的记录
List<Employee> employeeList = empService.searchEmp(map);
System.out.println(employeeList);
//设置员工的部门名称
for (Employee emp : employeeList) {
emp.setDeptName(deptService.findDeptById(emp.getDeptId()).getDeptName());
}
//得到总记录数
int total = employeeList.size();
//每页显示三条,总共显示的页数
int pageIndex = total / page.getCount();
pageIndex = total % page.getCount() == 0 ? pageIndex : pageIndex+1;
page.setTotalIndex(pageIndex);
page.calculateLast(total);
//将数据返回给页面
mv.addObject("empList",employeeList);
mv.addObject("page",page);
mv.addObject("total",total);
//返回部门信息列表页面
mv.setViewName("employee");
return mv;
}
/**
*
* @return
*/
@RequestMapping("/toAddEmp")
public ModelAndView toAddEmp(){
ModelAndView mv = new ModelAndView();
//添加信息到modelview。
List<Department> departmentList = deptService.findAllDepartment();
mv.addObject("deptList",departmentList);
mv.setViewName("addEmployee");
return mv;
}
/**
*
* @param employee
* @return
*/
@RequestMapping("/addEmp")
public ModelAndView addEmp(Employee employee){
ModelAndView mv = new ModelAndView();
//添加员工到数据库
empService.addEmp(employee);
mv.setViewName("redirect:/toEmployee");
return mv;
}
/**
*
* @param id
* @return
*/
@RequestMapping("/toModifyEmp")
public ModelAndView toUpdateEmp(int id){
ModelAndView mv = new ModelAndView();
//得到员工信息
mv.addObject("emp", empService.selectEmpById(id));
//得到所有部门信息
List<Department> departmentList = deptService.findAllDepartment();
mv.addObject("deptList",departmentList);
mv.setViewName("updateEmployee");
return mv;
}
/**
*
* @param employee
* @return
*/
@RequestMapping("/modifyEmp")
public ModelAndView modifyEmp(Employee employee){
ModelAndView mv = new ModelAndView();
//修改数据库中信息
empService.modifyEmp(employee);
List<Department> departmentList = deptService.findAllDepartment();
mv.addObject("deptList",departmentList);
//重定向到员工列表界面
mv.setViewName("redirect:/toEmployee");
return mv;
}
/**
*
* @param id
* @return
*/
@RequestMapping("/deleteEmp")
public ModelAndView deleteEmp(int id){
ModelAndView mv = new ModelAndView();
//删除员工
empService.deleteEmp(id);
mv.setViewName("redirect:/toEmployee");
return mv;
}
}

@ -1,5 +1,178 @@
package com.controller; package com.controller;
import com.entity.User;
import com.entity.Visitor;
import com.service.IUserService;
import com.service.IVisitorService;
import com.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName loginController
* @Description TODO
* @Author YHT
* @Date 2021/5/28 8:02
*/
@Controller
public class loginController { public class loginController {
@Autowired
private IUserService userService;
@Autowired
private IVisitorService visitorService;
/**
*
* @param username
* @param password
* @param request
* @return
*/
@RequestMapping("/main")
@ResponseBody
public ModelAndView toMain(String username, String password, HttpServletRequest request){
ModelAndView mv = new ModelAndView();
mv.setViewName("main");
//使用session存储数据
HttpSession session = request.getSession();
mv.addObject("status", "ok");
//根据用户名和密码查找用户
User user = userService.findByNameAndPwd(username, MD5Utils.stringToMD5(password));
//访客信息
Visitor visitor = new Visitor();
//用户不为空,则表示登录成功
if(user != null){
if(user.getName().equals(username) && user.getPassword().equals(MD5Utils.stringToMD5(password))){
//跳转至首页
mv.setViewName("main");
// mv.addObject("userName", user.getName());
// mv.addObject("status", "ok");
//存储数据
mv.addObject("userId", user.getId());
session.setAttribute("userName", user.getName());
session.setAttribute("userId", user.getId());
//添加访客信息
visitor.setName(user.getName());
visitor.setEmail(user.getEmail());
visitor.setIdentity(user.getIdentity());
visitor.setTelPhone(user.getTelNumber());
Date date = new Date(System.currentTimeMillis());
visitor.setVisitTime(date);
//存储方可信息
visitorService.addVisitor(visitor);
}else {
//查询失败,返回登录页
mv.setViewName("redirect:/index.jsp");
mv.addObject("status", "userNameOrPwdError");
}
}else{
//返回首页
mv.setViewName("redirect:/index.jsp");
mv.addObject("status", "userNameOrPwdError");
}
return mv;
}
/**
*
* @param user
* @return
*/
@RequestMapping("/register")
public String toRegister(User user){
//对用户密码进行加密
user.setPassword(MD5Utils.stringToMD5(user.getPassword()));
//将用户信息插入数据库
userService.insertUser(user);
//重定向到首页
return "redirect:/index.jsp";
}
/**
*
* @param id
* @return
*/
@RequestMapping("/toUserMsg")
public ModelAndView toUserMsg(Integer id){
ModelAndView mv = new ModelAndView();
//根据id找到用户
User user = userService.findById(id);
mv.setViewName("userMsg");
System.out.println(id);
System.out.println(user);
//将用户信息返回用户信息页
mv.addObject("loginUser", user);
return mv;
}
/**
*
* @param user
* @return
*/
@RequestMapping("/modifyUserMsg")
public ModelAndView modifyUserMsg(User user){
ModelAndView mv = new ModelAndView();
//修改用户信息
userService.modifyUser(user);
mv.addObject("loginUser", user);
//修改完成后,跳转至用户信息页
mv.setViewName("userMsg");
return mv;
}
/**
*
* @param id
* @return
*/
@RequestMapping("/toModifyPwd")
public ModelAndView toModifyPwd(int id){
ModelAndView mv = new ModelAndView();
//找到用户
User user = userService.findById(id);
mv.setViewName("modifyPwd");
mv.addObject("loginUser", user);
return mv;
}
/**
*
* @param id
* @param password
* @param rePassword
* @return
*/
@RequestMapping("/modifyPwd")
public ModelAndView modifyPwd(Integer id, String password, String rePassword){
ModelAndView mv = new ModelAndView();
//判断两次输入的密码是否一致
if(password.equals(rePassword)){
//一致。则更改数据库,重定向到登录页
userService.modifyPwd(password, id);
mv.setViewName("redirect:/index.jsp");
}else{
//跳转到修改密码页面
mv.setViewName("modifyPwd");
}
return mv;
}
/**
*
* @return
*/
@RequestMapping("/about")
public String about(){
return "about";
}
} }
Loading…
Cancel
Save