Springboot项目-归档+异常处理+登录拦截
2020-08-03 20:59
302 查看
1.归档功能
一、NewRepository
sql查询功能,来查询相关的归档数据
[code] @Query("select function('date_format',n.updateTime,'%Y')as year from News n group by year order by year desc ") List<String> findGroupYear(); @Query("select n from News n where function('date_format',n.updateTime,'%Y') = ?1") List<News> findByYear(String year);
二、NewService
服务功能的接口
[code] Map<String,List<News>> archiveNew(); Long countNew();
三、NewServiceImpl
用map结构将归档字符串和对应的数据存储起来
[code] @Override public Map<String, List<News>> archiveNew() { List<String> years = newRepository.findGroupYear(); Map<String,List<News>> map = new LinkedHashMap<>(); for (String year:years){ map.put(year,newRepository.findByYear(year)); System.out.println(year); } return map; } @Override public Long countNew() { return newRepository.count(); }
四、ArchiveShowController
[code]package com.whut.news.web; import com.whut.news.service.NewService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class ArchiveShowController { @Autowired private NewService newService; @GetMapping("/archives") public String archives(Model model){ model.addAttribute("archiveMap",newService.archiveNew()); model.addAttribute("newsCount",newService.countNew()); return "archives"; } }
2.异常处理功能
一、handler.ControllerExceptionHandler
[code]package com.whut.news.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getSession().getAttribute("user")==null){ response.sendRedirect("/admin"); return false; } return super.preHandle(request, response, handler); } }
二、NotFoundException
[code]package com.whut.news; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_FOUND) public class NotFoundException extends RuntimeException{ public NotFoundException(){ } public NotFoundException(String message) { super(message); } public NotFoundException(String message, Throwable cause) { super(message, cause); } }
3.登录拦截功能
一、LoginInterceptor
[code]package com.whut.news.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getSession().getAttribute("user")==null){ response.sendRedirect("/admin"); return false; } return super.preHandle(request, response, handler); } }
二、WebConfig
[code]package com.whut.news.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/admin/**") .excludePathPatterns("/admin") .excludePathPatterns("/admin/login"); } }
相关文章推荐
- 基于spring boot实现归档、异常处理、登录拦截
- spring boot新闻管理——异常处理和登录拦截
- [个人笔记]基于SpringBoot的新闻管理系统 归档展示/登录拦截/异常处理功能
- 【中软实训】12.归档+异常处理+登录拦截
- SpringBoot - shiro 作登录认证和拦截
- Spring Boot / Spring MVC 入门实践 (三) : 入门项目介绍与用户注册登录的实现
- 10.1更 spring boot 登录拦截(附上代码)
- spring boot 使用拦截器 实现 用户登录拦截
- spring boot 拦截器 登录权限拦截
- 使用spring boot登录拦截,静态资源一同拦截
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- 两个不同springboot+shiro的项目登录冲突
- 使用IDEA创建Spring Boot项目, 整合Mybatis ,连接MySql数据库,实现简单的登录注册功能
- 代码学习与实践 (一):Spring Boot 多模块项目创建与配置 注册 登录
- 异常处理(六)--------SpringBoot+Maven项目运行异常:Unable to find a single main class from the following candidat
- SpringBoot 整合Shiro 实现登录验证拦截功能
- SpringBoot+Shiro+Vue前后端分离项目通过JWT实现自动登录
- SpringBoot+Jpa+Mysql入门项目登录界面(含遇到的所有坑)
- Spring Boot + Vue 前后端分离项目如何踢掉已登录用户
- 【SpringBoot】在SpringBoot项目中使用拦截器进行登录态判断