登录、退出、个人中心、修改密码
关注点
- session操作
- 登录拦截器
Biz接口及其实现类
GlobalBiz
package com.imooc.oa.biz;
import com.imooc.oa.entity.Employee;
public interface GlobalBiz {
Employee login(String sn,String password);
void changePassword(Employee employee);
}
GlobalBizImpl
package com.imooc.oa.biz.impl;
import com.imooc.oa.biz.GlobalBiz;
import com.imooc.oa.dao.EmployeeDao;
import com.imooc.oa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("globalBiz")
public class GlobalBizImpl implements GlobalBiz {
@Autowired
private EmployeeDao employeeDao;
public Employee login(String sn, String password) {
Employee employee = employeeDao.select(sn);
if(employee!=null&&employee.getPassword().equals(password)){
return employee;
}
return null;
}
public void changePassword(Employee employee) {
employeeDao.update(employee);
}
}
控制器
package com.imooc.oa.controller;
import com.imooc.oa.biz.GlobalBiz;
import com.imooc.oa.entity.Employee;
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.RequestParam;
import javax.servlet.http.HttpSession;
@Controller("globalController")
public class GloablController {
@Autowired
private GlobalBiz globalBiz;
@RequestMapping("/to_login")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session, @RequestParam String sn, @RequestParam String password){
Employee employee = globalBiz.login(sn,password);
if (employee == null) {
return "redirect:to_login";
}
session.setAttribute("employee",employee);
return "redirect:self";
}
@RequestMapping("/self")
public String self(){
return "self";
}
@RequestMapping("/quit")
public String quit(HttpSession session){
session.setAttribute("employee",null);
return "redirect:to_login";
}
@RequestMapping("/to_change_password")
public String toChangePassword(){
return "change_password";
}
@RequestMapping("/change_password")
public String changePassword(HttpSession session, @RequestParam String old, @RequestParam String new1 ,@RequestParam String new2){
Employee employee = (Employee)session.getAttribute("employee");
if(employee.getPassword().equals(old)){
if(new1.equals(new2)){
employee.setPassword(new1);
globalBiz.changePassword(employee);
return "redirect:self";
}
}
return "redirect:to_change_password";
}
}
拦截器
package com.imooc.oa.global;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String url = httpServletRequest.getRequestURI();
if(url.toLowerCase().indexOf("login")>=0){
return true;
}
HttpSession session = httpServletRequest.getSession();
if(session.getAttribute("employee")!=null){
return true;
}
httpServletResponse.sendRedirect("/to_login");
return false;
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
在spring-web中加入
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.imooc.oa.global.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
页面
self.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 个人信息 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="col-md-2">工号</div>
<div class="col-md-4">${sessionScope.employee.sn}</div>
<div class="col-md-2">姓名</div>
<div class="col-md-4">${sessionScope.employee.name}</div>
</div>
<div class="section row">
<div class="col-md-2">所属部门</div>
<div class="col-md-4">${sessionScope.employee.department.name}</div>
<div class="col-md-2">职务</div>
<div class="col-md-4">${sessionScope.employee.post}</div>
</div>
<div class="panel-footer text-right">
<button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</div>
</div>
</div>
</section>
<jsp:include page="bottom.jsp"/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎使用爱慕课OA</title>
<meta name="keywords" content="HTML5 Bootstrap 3 Admin Template UI Theme" />
<meta name="description" content="AbsoluteAdmin - A Responsive HTML5 Admin UI Framework">
<meta name="author" content="AbsoluteAdmin">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="assets/img/favicon.ico">
</head>
<body class="external-page external-alt sb-l-c sb-r-c">
<div id="main" class="animated fadeIn">
<section id="content_wrapper">
<section id="content">
<div class="admin-form theme-info mw500" id="login">
<div class="content-header">
<h1> 爱慕课OA</h1>
<p class="lead">欢迎使用爱慕课自动化管理系统</p>
</div>
<div class="panel mt30 mb25">
<form method="post" action="login" id="contact">
<div class="panel-body bg-light p25 pb15">
<div class="section">
<label for="sn" class="field-label text-muted fs18 mb10">工号</label>
<label for="sn" class="field prepend-icon">
<input type="text" name="sn" id="sn" class="gui-input" placeholder="请输入工号...">
<label for="sn" class="field-icon">
<i class="fa fa-user"></i>
</label>
</label>
</div>
<div class="section">
<label for="password" class="field-label text-muted fs18 mb10">密码</label>
<label for="password" class="field prepend-icon">
<input type="password" name="password" id="password" class="gui-input" placeholder="请输入密码...">
<label for="password" class="field-icon">
<i class="fa fa-lock"></i>
</label>
</label>
</div>
</div>
<div class="panel-footer clearfix">
<button type="submit" class="button btn-primary mr10 pull-right">登陆</button>
<label class="switch ib switch-primary mt10">
<input type="checkbox" name="remember" id="remember" checked="true">
<label for="remember" data-on="是" data-off="否"></label>
<span>记住我</span>
</label>
</div>
</form>
</div>
</div>
</section>
</section>
</div>
<script src="vendor/jquery/jquery-1.11.1.min.js"></script>
<script src="vendor/jquery/jquery_ui/jquery-ui.min.js"></script>
<script src="assets/js/utility/utility.js"></script>
<script src="assets/js/demo/demo.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>
change_password.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 修改密码 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form method="post" action="change_password" id="admin-form">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="col-md-6">
<label for="old" class="field prepend-icon">
<input type="password" name="old" id="old" class="gui-input" placeholder="原始密码...">
<label for="old" class="field-icon">
<i class="fa fa-lock"></i>
</label>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-6">
<label for="new1" class="field prepend-icon">
<input type="password" name="new1" id="new1" class="gui-input" placeholder="重复密码...">
<label for="new1" class="field-icon">
<i class="fa fa-lock"></i>
</label>
</label>
</div>
<div class="col-md-6">
<label for="new2" class="field prepend-icon">
<input type="password" name="new2" id="new2" class="gui-input" placeholder="重复密码...">
<label for="new2" class="field-icon">
<i class="fa fa-lock"></i>
</label>
</label>
</div>
</div>
<div class="panel-footer text-right">
<button type="submit" class="button"> 修改 </button>
<button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<jsp:include page="bottom.jsp"/>