三层架构
- 持久层--Mybatis
- 表现层--Servlet + Jsp
- Spring--管理对象、切面处理
基于MVC模式
视图--Jsp
模型--JavaBean
-
控制器--Servlet+JavaBean
核心控制器
1、Servlet对象由Web容器管理
2、Service对象由IOC容器管理
- 持久层实现不用创建类,而是通过Mybatis配置文件实现的。
- controller通过注解配置servlet
部门管理
步骤
- 实体类
- Dao接口与Mybatis配置文件
- Service接口与其实现类
- 控制器
- 页面
日志模块
采用AOP切面处理
- applicationContext.xml
- mybatis 相关 xml
- DepartmentDao.xml
insert into department(name,address) values (#{name},#{address})
delete from department where id = #{id}
update department set name=#{name},address=#{address} where id = #{id}
- LogDao.xml
insert into log(opr_time,type,operator,moudle,operation,result)
values(#{oprTime},#{type},#{operator},#{moudle},#{operation},#{result})
- SelfDao.xml
- StaffDao.xml
insert into staff(account,password,status,did,name,sex,id_number,work_time,leave_time,born_date,info)
values(#{account},#{password},#{status},#{did},#{name},#{sex},#{idNumber},#{workTime},#{leaveTime},#{bornDate},#{info})
delete from staff where id=#{id}
update staff set account=#{account},password=#{password},status=#{status},
did=#{did},name=#{name},sex=#{sex},id_number=#{idNumber},
work_time=#{workTime},leave_time=#{leaveTime},born_date=#{bornDate},info=#{info} where id=#{id}
- DispatcherServlet 中央控制器
package com.alan.global;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DispatcherServlet extends GenericServlet {
private ApplicationContext applicationContext ;
public void init() throws ServletException {
applicationContext = new ClassPathXmlApplicationContext("spring.xml");
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
/**
* staff/add.do login.do
* staffController
*/
//或者请求的url路径,将最前面的/截取掉
String path = request.getServletPath().substring(1);
String beanName ;
String methodName;
int index = path.indexOf('/');
//包含反斜杠
if(index != -1){
beanName= path.substring(0,index)+"Controller"; //staffController
methodName = path.substring(index+1,path.indexOf(".do"));
}else {
beanName = "selfController"; //只有特殊的这一个
methodName = path.substring(0,path.indexOf(".do"));
}
System.out.println("path:"+path+",beanName:"+beanName+",methodName"+ methodName);
Object object = applicationContext.getBean(beanName);
try {
Method method = object.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(object,request,response);
}catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.getTargetException();
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
- 登陆过滤器
package com.alan.global;
import com.alan.entity.Staff;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 登陆过滤器
*/
public class LoginFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String path = request.getServletPath();
if(path.toLowerCase().indexOf("login") != -1){
//包含login的url,直接放行。不进行过滤处理
chain.doFilter(request,response);
}else {
Staff staff = (Staff) request.getSession().getAttribute("USER");
if(staff != null){
//session中有员工信息,直接放行,不进行过滤处理
chain.doFilter(request,response);
}else {
//强制跳转回登陆界面
response.sendRedirect(request.getContextPath()+"/toLogin.do");
}
}
}
public void init(FilterConfig config) throws ServletException {
}
}
- AOP切面类
package com.alan.global;
import com.alan.entity.Log;
import com.alan.entity.Staff;
import com.alan.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* 记录日志的切面类
*/
//@Component泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
@Component
//aspect切面注解
@Aspect
public class LogAdvice {
@Autowired
private LogService logService;
//配置运行日志,屏蔽掉SelfController,同时屏蔽to开头的例如toAdd等
@After("execution(* com.alan.controller.*.*(..)) &&!execution(* com.alan.controller.SelfController.*(..)) && !execution(* com.alan.controller.*.to*(..))")
public void operationLog(JoinPoint joinPoint){
Log log = new Log();
//模块名称赋值为类名
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
//当前操作赋值为方法名
log.setOperation(joinPoint.getSignature().getName());
//当前方法的第一个参数为request
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
Staff staff = (Staff) request.getSession().getAttribute("USER");
//设置操作人
log.setOperator(staff.getAccount());
log.setResult("成功");
logService.addOperationLog(log);
}
//配置系统日志,只有出现异常后进行记录,配置throwing
@AfterThrowing(throwing = "e" ,pointcut = "execution(* com.alan.controller.*.*(..)) && !execution(* com.alan.controller.SelfController.*(..))")
public void systemLog(JoinPoint joinPoint ,Throwable e){
Log log = new Log();
//模块名称赋值为类名
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
//当前操作赋值为方法名
log.setOperation(joinPoint.getSignature().getName());
//当前方法的第一个参数为request
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
Staff staff = (Staff) request.getSession().getAttribute("USER");
log.setOperator(staff.getAccount());
//将异常对象的名称传入到LOG中
log.setResult(e.getClass().getSimpleName());
logService.addSystemLog(log);
}
//配置登陆日志
@After("execution(* com.alan.controller.SelfController.login(..))")
public void loginLog(JoinPoint joinPoint){
log(joinPoint);
}
//配置退出日志,采用前置通知
@Before("execution(* com.alan.controller.SelfController.logout(..))")
public void logoutLog(JoinPoint joinPoint){
log(joinPoint);
}
private void log(JoinPoint joinPoint){
Log log = new Log();
//模块名称赋值为类名
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
//当前操作赋值为方法名
log.setOperation(joinPoint.getSignature().getName());
//当前方法的第一个参数为request
HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
Staff staff = (Staff) request.getSession().getAttribute("USER");
if(staff == null){
log.setOperator(request.getParameter("account"));
log.setResult("失败");
}else {
log.setOperator(staff.getAccount());
log.setResult("成功");
}
logService.addLoginLog(log);
}
}
- DepartmentController
package com.alan.controller;
import com.alan.entity.Department;
import com.alan.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller("departmentController")
public class DepartmentController extends HttpServlet {
@Autowired
private DepartmentService departmentService;
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = departmentService.getAll();
// Department department = departmentService.get(1);
request.setAttribute("LIST",list);
request.getRequestDispatcher("../department_list.jsp").forward(request,response);
}
public void toAdd(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
request.getRequestDispatcher("../department_add.jsp").forward(request,response);
}
public void add(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
//从request获取参数值
String name = request.getParameter("name");
String address = request.getParameter("address");
//将值复制给Department对象
Department department = new Department();
department.setName(name);
department.setAddress(address);
//插入表中
departmentService.add(department);
//返回list.do,不需要传值,使用重定向
response.sendRedirect("list.do");
}
public void toEdit(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
String id = request.getParameter("id");
Department department = new Department();
//根据id进行查询
department = departmentService.get(Integer.parseInt(id));
//将Department对象回传到更改页面
request.setAttribute("OBJ",department);
request.getRequestDispatcher("../department_edit.jsp").forward(request,response);
}
public void edit(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
//从request获取参数值
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
//将值复制给Department对象
Department department = new Department();
department.setName(name);
department.setAddress(address);
department.setId(Integer.parseInt(id));
//插入表中
departmentService.edit(department);
//返回list.do,不需要传值,使用重定向
response.sendRedirect("list.do");
}
public void remove(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
String id = request.getParameter("id");
departmentService.remove(Integer.parseInt(id));
response.sendRedirect("list.do");
}
}
- LogController
package com.alan.controller;
import com.alan.entity.Log;
import com.alan.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller("logController")
public class LogController {
@Autowired
private LogService logService;
public void operationLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = logService.getOperationLog();
request.setAttribute("LIST",list);
request.setAttribute("TYPE","操作");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
public void loginLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = logService.getLoginLog();
request.setAttribute("LIST",list);
request.setAttribute("TYPE","登陆");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
public void systemLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = logService.getSystemLog();
request.setAttribute("LIST",list);
request.setAttribute("TYPE","系统");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
}
- SelfController
package com.alan.controller;
import com.alan.entity.Staff;
import com.alan.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller("selfController")
public class SelfController {
//依赖注入对象
@Autowired
private SelfService selfService;
//跳转到登陆页面 jsp里面应该是toLogin.do
public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("login.jsp").forward(request,response);
}
//登陆流程 login.do
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从页面获取用户名和密码
String account = request.getParameter("account");
String password = request.getParameter("password");
//用户校验
Staff staff = selfService.login(account,password);
if(staff == null){
//密码错误,重定向到登陆页面。一般一些异常可能使用到重定向。url发生改变。
response.sendRedirect("toLogin.do");
}else {
request.getSession().setAttribute("USER",staff);
response.sendRedirect("main.do");
}
}
//打开主页面 main.do
public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("index.jsp").forward(request,response);
}
//退出 logout.do
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//将session中的USER置空
request.getSession().setAttribute("USER",null);
response.sendRedirect("toLogin.do");
}
// 显示个人信息 /self/info.do
public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("../info.jsp").forward(request,response);
}
//打开修改密码界面 /self/toChange.do
public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("../change_password.jsp").forward(request,response);
}
//修改密码 /self/changePassword.do
public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
Staff staff = (Staff) request.getSession().getAttribute("USER");
if(!staff.getPassword().equals(password)){
response.sendRedirect("toChangePassword.do");
}else {
selfService.changePassword(staff.getId(),password1);
// response.sendRedirect("../logout.do");
//
response.getWriter().print("");
}
}
}
- StaffController
package com.alan.controller;
import com.alan.entity.Department;
import com.alan.entity.Department;
import com.alan.entity.Staff;
import com.alan.service.DepartmentService;
import com.alan.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller("staffController")
public class StaffController {
@Autowired
private StaffService staffService;
@Autowired
private DepartmentService departmentService;
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = staffService.getAll();
request.setAttribute("LIST",list);
request.getRequestDispatcher("../staff_list.jsp").forward(request,response);
}
public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = departmentService.getAll();
request.setAttribute("DLIST",list);
request.getRequestDispatcher("../staff_add.jsp").forward(request,response);
}
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String account = request.getParameter("account");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String idNumber = request.getParameter("idNumber");
String info =request.getParameter("info");
Date bornDate=null;
try {
bornDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate"));
} catch (ParseException e) {
e.printStackTrace();
}
Integer did = Integer.parseInt(request.getParameter("did"));
Staff staff = new Staff();
staff.setInfo(info);
staff.setBornDate(bornDate);
staff.setIdNumber(idNumber);
staff.setDid(did);
staff.setAccount(account);
staff.setName(name);
staff.setSex(sex);
staffService.add(staff);
response.sendRedirect("list.do");
}
public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编辑界面需要员工信息和部门信息
Integer id = Integer.parseInt(request.getParameter("id"));
Staff staff = staffService.get(id);
request.setAttribute("OBJ",staff);
List list = departmentService.getAll();
request.setAttribute("DLIST",list);
request.getRequestDispatcher("../staff_edit.jsp").forward(request,response);
}
public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer.parseInt(request.getParameter("id"));
String account = request.getParameter("account");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String idNumber = request.getParameter("idNumber");
String info =request.getParameter("info");
Date bornDate=null;
try {
bornDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate"));
} catch (ParseException e) {
e.printStackTrace();
}
Integer did = Integer.parseInt(request.getParameter("did"));
Staff staff = staffService.get(id);
staff.setInfo(info);
staff.setBornDate(bornDate);
staff.setIdNumber(idNumber);
staff.setDid(did);
staff.setAccount(account);
staff.setName(name);
staff.setSex(sex);
staffService.edit(staff);
response.sendRedirect("list.do");
}
public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer.parseInt(request.getParameter("id"));
staffService.remove(id);
response.sendRedirect("list.do");
}
public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer.parseInt(request.getParameter("id"));
Staff staff = staffService.get(id);
request.setAttribute("OBJ",staff);
request.getRequestDispatcher("../staff_detail.jsp").forward(request,response);
}
}