mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
新增404页面,访问已删除的书页/目录页/内容页自动跳转到404页面
This commit is contained in:
parent
f61c252e71
commit
c9f1500976
@ -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());
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
BIN
novel-front/src/main/resources/static/images/404.jpeg
Normal file
BIN
novel-front/src/main/resources/static/images/404.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.5 KiB |
17
novel-front/src/main/resources/templates/404.html
Normal file
17
novel-front/src/main/resources/templates/404.html
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user