新增404页面,访问已删除的书页/目录页/内容页自动跳转到404页面

This commit is contained in:
xiongxiaoyang 2020-12-24 16:26:21 +08:00
parent f61c252e71
commit c9f1500976
5 changed files with 67 additions and 6 deletions

View File

@ -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());

View File

@ -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);
}
}

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

@ -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>