会议管理系统
一、使用技术
SSM+Freemark,项目来源:https://www.bilibili.com/video/BV1ep4y1S7s6?p=26&spm_id_from=333.851.header_right.history_list.click
二、基础准备工作
导入静态资源,编写service、domain、mapper
源码
三、实现
1.页面拦截
思路:通过获取请求过的地址和登录成功留下的session进行判断,若未登录则禁止进行跨权页面访问
/*页面拦截*/
public class PermInterceptor implements HandlerInterceptor {
//AntPathMatcher:类URLs字符串匹配
AntPathMatcher pathMatcher = new AntPathMatcher();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取请求过的地址,判断是否登录过
String requestURI = request.getRequestURI();
if ("/".equals(requestURI) || "/doLogin".equals(requestURI) || "/register".equals(requestURI) || "/doReg".equals(requestURI)){
return true;
}
//根据登录成功处留的session进行判断
HttpSession session = request.getSession(true);
Employee currentuser = (Employee) session.getAttribute("currentUser");
if (pathMatcher.match("/admin/**", requestURI)){
if (currentuser!=null){
if (currentuser.getRole()==2){
return true;
} else {
response.getWriter().write("forbidden");
return false;
}
}
} else {
if (currentuser != null){
return true;
}
}
//说明没有登录过,跳转至登录页
response.sendRedirect("/");
return false;
}
}
2.登录与员工注册、退出、修改密码
(1) 登录与员工注册
登录的思路:先判断用户是否存在,再判断用户状态,传递值给前端输出操作结果
注册的思路:接收用户传来的值 (使用员工类接收),若数据库已存在该用户则注册失败
@Controller
public class LoginController {
@Resource
private EmployeeService employeeService;
@Resource
private DepartmentService departmentService;
//将登录页设为首页
@RequestMapping("/")
public String login(){
return "login";
}
//登录
@PostMapping("/doLogin")
public String doLogin(String username, String password, Model model, HttpSession session){
Employee employee = employeeService.doLogin(username, password);
if (employee == null){
model.addAttribute("error", "用户名或密码错误,请重新登录");
return "forward:/";
} else {
if (employee.getStatus() == 0){
model.addAttribute("error", "用户处于待审批状态,登录失败");
return "forward:/";
} else if (employee.getStatus() == 2){
model.addAttribute("error", "用户审批未通过,登录失败");
return "forward:/";
} else {
session.setAttribute("currentUser", employee);
return "redirect:/notifications";
}
}
}
//页面跳转-注册
@RequestMapping("/register")
public String register(Model model){
//获取部门 (前端可实现)
List<Department> depsList = departmentService.getAllDeps();
model.addAttribute("deps", depsList);
return "register";
}
//实现注册
@RequestMapping("/doReg")
public String doReg(Employee employee, Model model){
Integer result = employeeService.doReg(employee);
if (result > 0){
return "redirect:/";
} else {
//注册失败,将当前的测试信息返回显示 (属性回填)
model.addAttribute("error", "注册失败,请重新输入");
model.addAttribute("employee", employee);
return "forward:/register";
}
}
}
--------------------在EmployeeServiceImpl实现类中进行登录和注册判断--------------------
/*用户登录*/
@Override
public Employee doLogin(String username, String password) {
Employee employee = mapper.loadEmpByUsername(username);
if (employee==null || !employee.getPassword().equals(password)){
return null;
}
return employee;
}
/*注册*/
@Override
public Integer doReg(Employee employee) {
Employee emp = mapper.loadEmpByUsername(employee.getUsername());
if (emp != null){
return -1;
}
employee.setRole(1);
employee.setStatus(0);
return mapper.doReg(employee);
}
(2) 退出
定义 LogoutController 控制类,移除掉session即可
@Controller
public class LogoutController {
/*用户退出*/
@RequestMapping("/logout")
public String logout(HttpServletRequest request) {
//将session从页面中移除
request.getSession().removeAttribute("currentUser");
return "redirect:/";
}
}
(3) 修改密码
定义 ChangePasswordController 控制类,通过用户传递过来的用户名和密码,查询是否有该用户,若有则更新密码
@Controller
public class ChangePasswordController {
@Resource
private EmployeeService employeeService;
/*页面跳转*/
@RequestMapping("/changepassword")
public String changehtml() {
return "changepassword";
}
/*修改密码*/
@PostMapping("/dochange")
public String dochang(String username, String password, String newpassword, Model model) {
Employee employee = employeeService.doLogin(username, password);
if (employee == null) {
model.addAttribute("error", "用户名或原密码输入错误,修改失败");
return "forward:changepassword";
} else {
employeeService.doChang(username, newpassword);
}
return "redirect:/";
}
}
3.注册审批
定义 ApproveaccountController 控制类,先显示所有注册了但是未经审批的员工,然后根据前端选中的员工和通过/不通过按钮,来更新员工状态
/*审批控制器*/
@Controller
@RequestMapping("/admin")
public class ApproveaccountController {
//alibaba不允许直接输入0,故用常量代替
public static final Integer PENDING_APPROVE = 0;
@Resource
private EmployeeService employeeService;
/*取出处于待审批状态的员工*/
@RequestMapping("/approveaccount")
public String approveaccount(Model model){
List<Employee> employeeList = employeeService.getAllEmpsByStatus(PENDING_APPROVE);
model.addAttribute("emps", employeeList);
return "approveaccount";
}
/*审批员工状态:通过/不通过*/
@RequestMapping("/updatestatus")
public String updatestatus(Integer employeeid, Integer status){
Integer result = employeeService.updatestatus(employeeid, status);
//无论是否通过,都返回审批页面
return "redirect:/admin/approveaccount";
}
}
4.★★搜索员工★★
分页操作通过与模糊查询放在一起处理 (mapper.xml处)
先获取员工总数,然后调用方法传入待查的员工信息、总页码、每页显示的数量,来获得一个员工列表,最后计算出页数,再把这些信息传递给前端进行遍历显示
@Controller
@RequestMapping("/admin")
public class EmployeeController {
//定义每页显示的数量
public static final Integer PAGE_SIZE = 5;
@Resource
private EmployeeService employeeService;
/*【分页与模糊查询】员工,分页一般与模糊查询一起处理*/
@RequestMapping("/searchemployees")
public String searchemployees(Employee employee, @RequestParam(defaultValue = "1")Integer page, Model model){
List<Employee> emps = employeeService.getAllEmps(employee, page, PAGE_SIZE);
//获取员工总记录数
Long total = employeeService.getTotal(employee);
model.addAttribute("emps", emps); //所有员工
model.addAttribute("total", total); //员工总数
model.addAttribute("page", page); //页码
model.addAttribute("pagenum", total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1); //总共可以分成多少页
return "searchemployees";
}
/*关闭账号*/
@RequestMapping("/updateemp")
public String updateemp(Integer id){
employeeService.updatestatus(id, 2);
return "redirect:/admin/searchemployees?status=1";
}
}
<!--【分页与模糊查询】员工-->
<select id="getAllEmps" resultType="employee">
select *
from employee where status = #{emp.status}
<if test="emp.employeeName!=null">
and employeename like concat('%',#{emp.employeeName},'%')
</if>
<if test="emp.username!=null">
and username like concat('%', #{emp.username},'%')
</if>
limit #{page},#{pagesize}
</select>
<div class="page-content">
<div class="content-nav">