这篇文章我们将改写图书管理系统为强制登录版本。我们需要结合在统一功能中讲解的
拦截器、统一数据返回、统一异常处理。内容完整的再写一遍图书管理系统
一、实现强制登录
1.1自定义拦截器
1.2注册配置拦截器
完整LoginInterceptor类代码
/**
* 拦截器
* 实现强制登录
*/
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
/**
* 处理请求前执行。
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("目标方法执行前执行:LoginInterceptor.preHandle...");
//true-放行。false拦截
//验证用户是否登录
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
if(userInfo == null || userInfo.getId()<1){
response.sendRedirect("/login.html");
return false;
}
return true;
}
/**
* 接口处理完成之后执行的方法
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
}
完整WebConfig类代码
package com.qiyangyang.springbook.demos.Config;
import com.qiyangyang.springbook.demos.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays;
import java.util.List;
//配合拦截器进行拦截处理
//注意一定要加上@Configuration这个注解 才会生效
@Configuration
public class WebConfig implements WebMvcConfigurer {
private List<String> excludePath = Arrays.asList(
"/user/login",
"/**/login.html",
"/css/**",
"/js/**",
"/pic/**"
);
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// /**表示对所有路径生效
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
}
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// // /** 表示对所有的路径生效
// registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
// //排除一些路径
// .excludePathPatterns("/user/login")
// .excludePathPatterns("/**/**.html")
// .excludePathPatterns("/css/**")
// .excludePathPatterns("/js/**")
// .excludePathPatterns("/pic/**");
// }
}
二、实现统一返回数据
2.1创建Result类
package com.qiyangyang.springbook.demos.model;
import lombok.Data;
/**
* 定义统一返回数据
* 对返回结果进行细致化描述
* @param <T>
*/
@Data
public class Result <T>{
private Integer code;//后端响应状态码,业务状态码 200成功,-1失败,-2表示未登录
private String Errmsg; //后端发生错误的原因
private T data; //返回的数据
public static <T>Result<T> success(T data){
Result<T> result = new Result<T>();
result.setCode(200);
result.setData(data);
return result;
}
public static <T>Result<T> fail(T data,String errmg){
Result<T> result = new Result<T>();
result.setCode(-1);
result.setErrmsg(errmg);
result.setData(data);
return result;
}
public static <T> Result<T> fail(String errMsg){
Result<T> result = new Result<T>();
result.setCode(-1);
result.setErrmsg(errMsg);
return result;
}
/**
* 未登录时
*/
public static <T> Result<T> unlogin(){
Result<T> result = new Result<T>();
result.setCode(-2);
result.setErrmsg("用户未登录");
return result;
}
}
创建ResponseAdvice并实现ResponseBodyAdvice接口
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
@Autowired
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
//重写supports方法。看对哪些请求进行处理。返回true--进行处理 返回false。不进行处理
return true;
}
@SneakyThrows
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//重写beforeBodyWrite方法。如果body是Result类型。不再进行包装。如果不是。那么就进行包装
//接口执行完成之后,返回结果之前会执行这个方法。
//注意需要处理String类型的
if (body instanceof Result){
return body;
}
if (body instanceof String){
return objectMapper.writeValueAsString(Result.success(body));
}
return Result.success(body);
}
}
三、修改前端代码
登录页代码
图书列表显示页代码
function getBookList() {
//ajax页面一加载就会被执行的程序。 我们还可以进行方法封装
$.ajax({
type: "get",
url: "/book/getListByPage" + location.search,
success: function (result) {
var books = result.data.records;
console.log(books); //如果前端没有报错,那么我们打印日志。观察后端返回结果对不对
var findHtml = ""; //用这个变量来拼接HTML
for (var book of books) {
//拼接html。假如后端返回10个tr那么直接for循环拼接在这里面。findHtml
//我们用单引号拼接,因为里面有双引号
图书分页代码
前端添加图书代码
我们照上面修改
将
success: function (result) {
console.log(result.data);
if (result == "成功添加图书") {
alert("添加成功");
location.href = "book_list.html";
} else {
alert(result.data);
}
修改为
success: function (result) {
console.log(result.data);
if (result.code ==200 && result.data == "成功添加图书") {
alert("添加成功");
location.href = "book_list.html";
} else {
alert(result.data);
}
因此我们将
@RequestMapping("/addBook")
public String addBook(BookInfo bookInfo){
log.info("添加图书,bookInfo:{}",bookInfo);
/**
修改为
@RequestMapping(value = "/addBook",produces = "application/json")
public String addBook(BookInfo bookInfo){
log.info("添加图书,bookInfo:{}",bookInfo);
/**
@RequestMapping( "/addBook")
public Result addBook(BookInfo bookInfo){
log.info("添加图书,bookInfo:{}",bookInfo);
/**
* 参数校验
*/
if(!StringUtils.hasLength(bookInfo.getBookName())
|| !StringUtils.hasLength(bookInfo.getAuthor())
|| !StringUtils.hasLength(bookInfo.getPublish())
|| bookInfo.getCount() <=0
|| bookInfo.getPrice()==null){
return Result.fail("参数错误!!!");
}
/**
* 添加图书
*/
/**
* 出现错误比如参数错误。添加过程出现异常,因为MySQL会出现一些异常
* 我们使用try/catch来捕获。
*/
try {
bookService.insertBook(bookInfo);
}catch (Exception e){
log.error("添加图书失败,e:{}",e);
return Result.fail("内部发生错误,请联系管理员!");
}
/**
* 返回成功添加吐过表示图书插入成功。
*/
return Result.success("成功添加图书");
}
修改图书代码
//查询当前ID图书
$.ajax({
type: "get",
url: "book/queryBookById"+location.search,
success:function(book){
if(book!=null){
$("#bookId").val(book.id);
$("#bookName").val(book.bookName);
$("#bookAuthor").val(book.author);
$("#bookStock").val(book.count);
$("#bookPrice").val(book.price);
$("#bookPublisher").val(book.publish);
$("#bookStatus").val(book.status);
}
}
type: "get",
url: "book/queryBookById"+location.search,
success:function(result){
//前端根据后端返回结果,针对不同的情况进行处理
if(result.code == 200 && result.data != null){
var book = result.data;
if(book!=null){
$("#bookId").val(book.id);
$("#bookName").val(book.bookName);
$("#bookAuthor").val(book.author);
$("#bookStock").val(book.count);
$("#bookPrice").val(book.price);
$("#bookPublisher").val(book.publish);
$("#bookStatus").val(book.status);
}
}
type: "get",
url: "/book/updateBook",
data:$("#updateBook").serialize(),//提交整个表单
success:function(result){
if(result.code==200 && result.data == true){
alert("更新成功");
location.href = "book_list.html"
}else{
alert("更新失败");
}
删除图书代码
if(result == true){
alert("删除成功");
location.href = "book_list.html";
}
if(result.code ==200 && result.data == true){
alert("删除成功");
location.href = "book_list.html";
}
批量删除图书代码
if(result == true){
alert("批量删除成功");
location.href = "book_list.html";
}else{
alert("删除失败,请联系管理员!");
}
修改为
if(result.code ==200 && result.data == true){
alert("批量删除成功");
location.href = "book_list.html";
}else{
alert("删除失败,请联系管理员!");
}
四、统一异常处理
@ControllerAdvice
public class ErrorHandler {
/**
* 捕获异常,返回统一结果
*/
//通过@ExceptionHandler这个注解来捕获异常
@ExceptionHandler
public Result handler(Exception e){
return Result.fail(e.getMessage());
}
}