人事管理系统项目(完)
主要功能写完后还需要写处理异常的Exception类,建一个exception包,包下有两个处理异常的类;BusinessException类用来处理我自定义的异常,SysException类用来处理系统异常,代码如下:
BusinessException:
package com.jiazhong.hr.exception;
public class BusinessException extends RuntimeException{
public BusinessException() {
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
SysException:
package com.jiazhong.hr.exception;
public class SysException extends Exception{
public SysException() {
}
public SysException(String message) {
super(message);
}
public SysException(String message, Throwable cause) {
super(message, cause);
}
public SysException(Throwable cause) {
super(cause);
}
public SysException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
写完处理异常的类再来写过滤器的类,EncodingFilter是编码过过滤器,这个过滤器会设置字符编码为UTF-8,PowerFilter是权限过滤器,这个过滤器是加了一个权限让未登录的用户不允许访问然后自动跳转到登录页面,下面来看下代码:
EncodingFilter:
package com.jiazhong.hr.filters;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: 加中实训
* @Date:2020/9/23 10:53
* @Description: 编码过滤器
*/
@WebFilter("/*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request1, ServletResponse response1, FilterChain chain) throws IOException, ServletException {
System.out.println("编码过滤器被执行...");
//将ServletRequest对象转换为基于Http协议的HttpServletRequest对象
HttpServletRequest request = (HttpServletRequest)request1;
HttpServletResponse response = (HttpServletResponse)response1;
//设置请求编码集
request.setCharacterEncoding("UTF-8");
//设置响应类型及编码
//response.setContentType("json/application;charset=utf-8");
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
PowerFilter:
package com.jiazhong.hr.filters;
import com.jiazhong.hr.commons.Constant;
import com.jiazhong.hr.commons.CurAccount;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: 加中实训
* @Date:2020/9/23 11:07
* @Description:
* 权限过滤器,未登录用于不允许访问自动跳转到登录页面
*/
@WebFilter("/emp/*")
public class PowerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request1, ServletResponse response1, FilterChain chain) throws IOException, ServletException {
System.out.println("权限过滤器被执行...");
//将ServletRequest对象转换为基于Http协议的HttpServletRequest对象
HttpServletRequest request = (HttpServletRequest)request1;
HttpServletResponse response = (HttpServletResponse)response1;
//从session中获得账户对象
CurAccount curAccount = (CurAccount) request.getSession().getAttribute(Constant.SESSION_ATTR_CUR_ACCOUNT);
//如果curAccount为null表示用户未登录
if(curAccount == null){
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("<script>alert('未登录不允许访问');location='/';</script>");
//跳转到登录页面进行登录
//response.sendRedirect("/");
return;
}
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
最后封装一个公共包,这个包下封装几个类,Result类封装的是结果集,所有的结果封装到这里;CurAccount类封装所有登录者的账户信息;PageParams类存储了分页需要的相关参数;Constant类封装了一个固定的名字,下面是代码:
Constant:
package com.jiazhong.hr.commons;
public class Constant {
public static final String SESSION_ATTR_CUR_ACCOUNT="SESSION_ATTR_CUR_ACCOUNT";
}
CurAccount:
package com.jiazhong.hr.commons;
/**
* @Author: 加中实训
* @Date:2020/8/21 14:24
* @Description: 封装当前登录者的账户信息
*/
public class CurAccount {
private int account_id;
private String account_name;
private int emp_id;
public CurAccount() {
}
public CurAccount(int account_id, String account_name, int emp_id) {
this.account_id = account_id;
this.account_name = account_name;
this.emp_id = emp_id;
}
public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
this.account_id = account_id;
}
public String getAccount_name() {
return account_name;
}
public void setAccount_name(String account_name) {
this.account_name = account_name;
}
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
}
PageParams:
package com.jiazhong.hr.commons;
/**
* @Author: 加中实训
* @Date:2020/9/16 11:27
* @Description:
* 分页参数类
* 该类中存储分页所需要的相关参数
*/
public class PageParams {
private Integer pageNumber;//要查询的页码
private Integer pageSize;//每页显示的记录数
public PageParams() {
}
public PageParams(Integer pageNumber, Integer pageSize) {
this.pageNumber = pageNumber;
this.pageSize = pageSize;
}
public Integer getPageNumber() {
return pageNumber;
}
public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
Result:
package com.jiazhong.hr.commons;
public class Result {
private boolean success;
private String message;
private Object data;
private Result(boolean success, String message, Object data) {
this.success = success;
this.message = message;
this.data = data;
}
public static Result success(){
return new Result(true,null,null);
}
public static Result success(String message){
return new Result(true,message,null);
}
public static Result success(String message,Object data){
return new Result(true,message,data);
}
public static Result fail(){
return new Result(false,null,null);
}
public static Result fail(String message){
return new Result(false,message,null);
}
public static Result fail(String message,Object data){
return new Result(false,message,data);
}
public boolean isSuccess() {
return success;
}
public String getMessage() {
return message;
}
public Object getData() {
return data;
}
}
好了,到这里这个项目就已经完美结束了,这个项目实现的功能不是很多,毕竟只是一个小项目,但是里面需要用到的很多技术很值得学习,所以在这里分享给大家互相交流,希望能帮到有需要的人,最后感谢大家了解我这个项目,如果有需要改进的地方欢迎随时评论。