From 16e0687b4e3928ef7464701dceb0269a5f986a15 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Sun, 23 Oct 2022 00:43:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=BC=82=E5=B8=B8=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=8F=8A=E8=87=AA=E5=AE=9A=E4=B9=89=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/.ehcache-diskstore.lock | 0 cache/blog-cache.data | Bin 0 -> 15980 bytes .../cc/bnblogs/Exception/CustomException.java | 8 +++ .../cc/bnblogs/config/ExceptionConfig.java | 33 ++++++++-- .../java/cc/bnblogs/config/WebConfig.java | 21 +++++++ .../bnblogs/controller/IndexController.java | 57 +++++++++++++++--- .../java/cc/bnblogs/enums/ErrorCodeEnum.java | 20 ++++++ src/main/resources/static/css/error.css | 4 +- .../templates/{error.html => fail.html} | 11 ++-- 9 files changed, 134 insertions(+), 20 deletions(-) create mode 100644 cache/.ehcache-diskstore.lock create mode 100644 cache/blog-cache.data create mode 100644 src/main/java/cc/bnblogs/Exception/CustomException.java create mode 100644 src/main/java/cc/bnblogs/enums/ErrorCodeEnum.java rename src/main/resources/templates/{error.html => fail.html} (82%) diff --git a/cache/.ehcache-diskstore.lock b/cache/.ehcache-diskstore.lock new file mode 100644 index 0000000..e69de29 diff --git a/cache/blog-cache.data b/cache/blog-cache.data new file mode 100644 index 0000000000000000000000000000000000000000..dfdf1f28cbd51d02cdec80dee755d41bf47c4000 GIT binary patch literal 15980 zcmds8dvH_NnZKB4OiTk|6VitE252A@NtPe7pl!;t3B(1;W76ia=t{bhu90*V-77yb zI~|)~Fb`u>9t~iJmqWY>*pQcv4Y<3V?e288olbZ9Pdl^SX>_mT-EFhe*+-`{+wSi> z=SsSg4GGh1cN32->z;GI?|k1m-}{_<`B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;jVvQ}cY_{n~}= defaultException(Exception e) { + public void defaultException(Exception e, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + e.printStackTrace(); + log.error(e.getMessage(), e); + // 返回的是页面 + if (request.getRequestURI().endsWith(".html")) { + request.getRequestDispatcher("/error/500.html").forward(request, response); + } else { + // 返回的是json + response.setCharacterEncoding("utf-8"); + response.getWriter().print(new ObjectMapper().writeValueAsString(Result.error())); + } + } + + /** + * http状态码虽然是200,当业务上有错误(比如访问的文章、分类、标签不存在时) + * 当业务上存在404时,统一转发到/error/404.html + * @param e 自定义错误异常类 + * @return 404.html + */ + @ExceptionHandler(CustomException.class) + public String notFoundException(CustomException e) { e.printStackTrace(); log.error(e.getMessage(),e); - return Result.error(); + return "forward:/error/404.html"; } } diff --git a/src/main/java/cc/bnblogs/config/WebConfig.java b/src/main/java/cc/bnblogs/config/WebConfig.java index 10634a6..7d8cc3a 100644 --- a/src/main/java/cc/bnblogs/config/WebConfig.java +++ b/src/main/java/cc/bnblogs/config/WebConfig.java @@ -1,13 +1,22 @@ package cc.bnblogs.config; import cc.bnblogs.common.LRUCache; +import cc.bnblogs.common.Result; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.server.ConfigurableWebServerFactory; +import org.springframework.boot.web.server.ErrorPage; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.File; /** @@ -41,6 +50,18 @@ public class WebConfig implements WebMvcConfigurer { // 记住在absolutePath前加一个'/',否则本地图片读取不出来(重要的事情说三遍) registry.addResourceHandler("/upload/**").addResourceLocations("file://" + '/' + absolutePath); } + + /** + * 错误页面配置是根据HTTP请求返回的状态码决定的(Http异常) + * Controller层的报错是thymeleaf渲染页面时报错的(业务异常) + * @return 跳转到对应的error页面 + */ + @Bean + public WebServerFactoryCustomizer webServerFactoryCustomizer() { + return factory -> factory.addErrorPages( + new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html"), + new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html")); + } @Bean public LRUCache lruCache() { return new LRUCache(MAX_LRU_CACHE_SIZE); diff --git a/src/main/java/cc/bnblogs/controller/IndexController.java b/src/main/java/cc/bnblogs/controller/IndexController.java index 27b60b1..a3153d3 100644 --- a/src/main/java/cc/bnblogs/controller/IndexController.java +++ b/src/main/java/cc/bnblogs/controller/IndexController.java @@ -1,7 +1,9 @@ package cc.bnblogs.controller; +import cc.bnblogs.Exception.CustomException; import cc.bnblogs.common.*; import cc.bnblogs.config.WebConfig; +import cc.bnblogs.enums.ErrorCodeEnum; import cc.bnblogs.enums.ResultEnum; import cc.bnblogs.pojo.Article; import cc.bnblogs.pojo.Category; @@ -99,10 +101,13 @@ public class IndexController { @GetMapping("/tag/{id}.html") public String tagList(@PathVariable Integer id, @RequestParam(required = false,defaultValue = "1") Integer pageNumber, - Model model){ + Model model) throws CustomException { Tag tag = tagService.detail(id); + if (Objects.isNull(tag)) { + throw new CustomException(); + } model.addAttribute("tag",tag); - // todo id不存在时要特殊处理 + ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); articleSearch.setTid(id); PageHelper
articlePage = articleService.search(articleSearch); @@ -123,10 +128,12 @@ public class IndexController { @GetMapping("/category/{id}.html") public String categoryList(@PathVariable Integer id, @RequestParam(required = false,defaultValue = "1") Integer pageNumber, - Model model){ + Model model) throws CustomException { Category category = categoryService.detail(id); + if (Objects.isNull(category)) { + throw new CustomException(); + } model.addAttribute("category",category); - // todo 分类不存在时要特殊处理 ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); articleSearch.setCid(id); PageHelper
articlePage = articleService.search(articleSearch); @@ -178,22 +185,54 @@ public class IndexController { return "tags"; } - @GetMapping("/error") - public String error() { - return "error"; + /** + * 404错误请求 + * @param model MVC model + * @return 错误页 + */ + @GetMapping("/error/404.html") + public String error404(Model model) { + model.addAttribute("error", ErrorCodeEnum.ERROR_404); + return "fail"; + } + + /** + * 500错误请求 + * @param model MVC model + * @return 错误页 + */ + @GetMapping("/error/500.html") + public String error500(Model model) { + model.addAttribute("error", ErrorCodeEnum.ERROR_500); + return "fail"; } + /** + * 文章详情页 + * @param id 文章id + * @param model MVC model + * @param request HttpServletRequest请求 + * @param response HttpServletResponse响应 + * @return + */ + @GetMapping("/{id}.html") public String detail(@PathVariable Integer id, Model model, - HttpServletRequest request, HttpServletResponse response) { + HttpServletRequest request, HttpServletResponse response) + throws CustomException { Article article = articleService.detail(id); + // 文章不存在 + if (Objects.isNull(article)) { + throw new CustomException(); + } + // 如果cookie不存在 if (Objects.isNull(CookieUtil.getCookie(request,Article.VIEW_PREFIX + id))) { // 更新文章浏览量 articleService.viewArticle(id); CookieUtil.setCookie(response,Article.VIEW_PREFIX + id,"true"); } - // todo 文章内容不存在时应该抛出异常 + model.addAttribute("article",article); PageHelper commentPage = commentService.show(id,1); // 返回第一页评论 diff --git a/src/main/java/cc/bnblogs/enums/ErrorCodeEnum.java b/src/main/java/cc/bnblogs/enums/ErrorCodeEnum.java new file mode 100644 index 0000000..4033389 --- /dev/null +++ b/src/main/java/cc/bnblogs/enums/ErrorCodeEnum.java @@ -0,0 +1,20 @@ +package cc.bnblogs.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/22 + */ + +@Getter +@AllArgsConstructor +public enum ErrorCodeEnum { + // HTTP请求的状态码和返回信息 + ERROR_404(404,"对不起,你请求的页面暂时未找到.
它或许已经被迁移至其它页面啦."), + ERROR_500(500,"您好,服务器暂时出发问题,请稍候访问.
或您也可以尝试联系站长."),; + private final Integer code; + private final String msg; +} diff --git a/src/main/resources/static/css/error.css b/src/main/resources/static/css/error.css index 4f3b3e7..05188bc 100644 --- a/src/main/resources/static/css/error.css +++ b/src/main/resources/static/css/error.css @@ -247,7 +247,9 @@ sup { display: block; top: 3rem; left: 3rem; - width: 5rem; + width: 20rem; + font-weight: 500; + font-size: larger; } .logo-link .logo { width: 100%; diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/fail.html similarity index 82% rename from src/main/resources/templates/error.html rename to src/main/resources/templates/fail.html index c630fb5..6b4c1e5 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/fail.html @@ -1,5 +1,5 @@  - + Title @@ -13,7 +13,7 @@
-冷文学习者 +
@@ -47,10 +47,9 @@
-

错误码:404

+

错误码:

-

对不起,你请求的页面暂时未找到.
- 它或许已经被迁移至其它页面啦.

+

@@ -59,7 +58,7 @@