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 0000000..dfdf1f2 Binary files /dev/null and b/cache/blog-cache.data differ diff --git a/src/main/java/cc/bnblogs/Exception/CustomException.java b/src/main/java/cc/bnblogs/Exception/CustomException.java new file mode 100644 index 0000000..2ecadce --- /dev/null +++ b/src/main/java/cc/bnblogs/Exception/CustomException.java @@ -0,0 +1,8 @@ +package cc.bnblogs.Exception; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/23 + */ +public class CustomException extends Exception{ +} diff --git a/src/main/java/cc/bnblogs/config/ExceptionConfig.java b/src/main/java/cc/bnblogs/config/ExceptionConfig.java index fd9ca3e..337cbfc 100644 --- a/src/main/java/cc/bnblogs/config/ExceptionConfig.java +++ b/src/main/java/cc/bnblogs/config/ExceptionConfig.java @@ -1,10 +1,16 @@ package cc.bnblogs.config; +import cc.bnblogs.Exception.CustomException; import cc.bnblogs.common.Result; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; /** * @author zfp@bnblogs.cc @@ -14,11 +20,30 @@ import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class ExceptionConfig { - @ResponseBody @ExceptionHandler(Exception.class) - public Result 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 @@