diff --git a/novel-front/src/main/java/com/java2nb/novel/controller/PageController.java b/novel-front/src/main/java/com/java2nb/novel/controller/PageController.java index aa6ba24..23f69ca 100644 --- a/novel-front/src/main/java/com/java2nb/novel/controller/PageController.java +++ b/novel-front/src/main/java/com/java2nb/novel/controller/PageController.java @@ -9,6 +9,7 @@ import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.NewsService; import com.java2nb.novel.service.UserService; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; @@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.util.List; @@ -97,9 +99,14 @@ public class PageController extends BaseController{ /** * 详情页 * */ + @SneakyThrows @RequestMapping("/book/{bookId}.html") - public String bookDetail(@PathVariable("bookId") Long bookId, Model model) { + public String bookDetail(@PathVariable("bookId") Long bookId, HttpServletResponse resp, Model model) { Book book = bookService.queryBookDetail(bookId); + if(book == null){ + resp.sendError(HttpServletResponse.SC_NOT_FOUND); + return null; + } model.addAttribute("book",book); if(book.getLastIndexId() != null) { //查询首章目录ID @@ -112,9 +119,14 @@ public class PageController extends BaseController{ /** * 目录页 * */ + @SneakyThrows @RequestMapping("/book/indexList-{bookId}.html") - public String indexList(@PathVariable("bookId") Long bookId, Model model) { + public String indexList(@PathVariable("bookId") Long bookId, HttpServletResponse resp, Model model) { Book book = bookService.queryBookDetail(bookId); + if(book == null){ + resp.sendError(HttpServletResponse.SC_NOT_FOUND); + return null; + } model.addAttribute("book",book); List<BookIndex> bookIndexList = bookService.queryIndexList(bookId,null,1,null); model.addAttribute("bookIndexList",bookIndexList); @@ -125,13 +137,18 @@ public class PageController extends BaseController{ /** * 内容页 * */ + @SneakyThrows @RequestMapping("/book/{bookId}/{bookIndexId}.html") - public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request,Model model) { + public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, HttpServletResponse resp,Model model) { //查询书籍 Book book = bookService.queryBookDetail(bookId); - model.addAttribute("book",book); //查询目录 BookIndex bookIndex = bookService.queryBookIndex(bookIndexId); + if(book == null || bookIndex == null){ + resp.sendError(HttpServletResponse.SC_NOT_FOUND); + return null; + } + model.addAttribute("book",book); model.addAttribute("bookIndex",bookIndex); //查询上一章节目录ID Long preBookIndexId = bookService.queryPreBookIndexId(bookId,bookIndex.getIndexNum()); diff --git a/novel-front/src/main/java/com/java2nb/novel/core/config/ErrorPageConfig.java b/novel-front/src/main/java/com/java2nb/novel/core/config/ErrorPageConfig.java new file mode 100644 index 0000000..1dd2d6f --- /dev/null +++ b/novel-front/src/main/java/com/java2nb/novel/core/config/ErrorPageConfig.java @@ -0,0 +1,25 @@ +package com.java2nb.novel.core.config; + +import org.springframework.boot.web.server.ErrorPage; +import org.springframework.boot.web.server.ErrorPageRegistrar; +import org.springframework.boot.web.server.ErrorPageRegistry; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; + +/** + * 错误页面配置 + * @author xiongxiaoyang + */ +@Configuration +public class ErrorPageConfig implements ErrorPageRegistrar { + + @Override + public void registerErrorPages(ErrorPageRegistry registry) { + /*1.错误类型为404,默认显示404.html网页*/ + ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"); + /** + TODO 2.错误类型为500,表示服务器响应错误,默认显示/500.html网页 + */ + registry.addErrorPages(e404); + } +} diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java index baae58b..6996981 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java @@ -235,7 +235,8 @@ public class BookServiceImpl implements BookService { .where(id, isEqualTo(bookId)) .build() .render(RenderingStrategies.MYBATIS3); - return bookMapper.selectMany(selectStatement).get(0); + List<Book> books = bookMapper.selectMany(selectStatement); + return books.size() > 0 ? books.get(0) : null; } @Override @@ -264,7 +265,8 @@ public class BookServiceImpl implements BookService { .where(BookIndexDynamicSqlSupport.id, isEqualTo(bookIndexId)) .build() .render(RenderingStrategies.MYBATIS3); - return bookIndexMapper.selectMany(selectStatement).get(0); + List<BookIndex> bookIndices = bookIndexMapper.selectMany(selectStatement); + return bookIndices.size() > 0 ? bookIndices.get(0) : null; } @Override diff --git a/novel-front/src/main/resources/static/images/404.jpeg b/novel-front/src/main/resources/static/images/404.jpeg new file mode 100644 index 0000000..1308714 Binary files /dev/null and b/novel-front/src/main/resources/static/images/404.jpeg differ diff --git a/novel-front/src/main/resources/templates/404.html b/novel-front/src/main/resources/templates/404.html new file mode 100644 index 0000000..47cb326 --- /dev/null +++ b/novel-front/src/main/resources/templates/404.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Page Not Found</title> + <script> + setTimeout(function () { + location.href = '/'; + },3000) + + </script> +</head> +<body style="background: url(/images/404.jpeg) no-repeat;" > + + +</body> +</html> \ No newline at end of file