页面不存在则跳转到404页面

This commit is contained in:
xiongxiaoyang 2020-12-24 16:45:48 +08:00
parent c9f1500976
commit 7494fe431a
3 changed files with 33 additions and 21 deletions

View File

@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
* *
* @author 11797*/ * @author 11797*/
@Slf4j @Slf4j
@RestControllerAdvice @RestControllerAdvice(basePackages = "com.java2nb.novel.controller")
public class CommonExceptionHandler { public class CommonExceptionHandler {
/** /**

View File

@ -0,0 +1,26 @@
package com.java2nb.novel.core.advice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* 页面异常处理器
*
* @author 11797
*/
@Slf4j
@ControllerAdvice(basePackages = "com.java2nb.novel.page")
public class PageExceptionHandler {
/**
* 处理所有异常
*/
@ExceptionHandler(Exception.class)
public String handlerException(Exception e) {
log.error(e.getMessage(), e);
//跳转页面过程中出现异常时统一跳转到404页面
return "404";
}
}

View File

@ -1,6 +1,6 @@
package com.java2nb.novel.controller; package com.java2nb.novel.page;
import com.java2nb.novel.core.bean.ResultBean; import com.java2nb.novel.controller.BaseController;
import com.java2nb.novel.core.bean.UserDetails; import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.core.utils.ThreadLocalUtil; import com.java2nb.novel.core.utils.ThreadLocalUtil;
import com.java2nb.novel.entity.*; import com.java2nb.novel.entity.*;
@ -15,12 +15,10 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
/** /**
@ -29,7 +27,7 @@ import java.util.List;
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
@Controller @Controller
public class PageController extends BaseController{ public class PageController extends BaseController {
private final BookService bookService; private final BookService bookService;
@ -101,12 +99,8 @@ public class PageController extends BaseController{
* */ * */
@SneakyThrows @SneakyThrows
@RequestMapping("/book/{bookId}.html") @RequestMapping("/book/{bookId}.html")
public String bookDetail(@PathVariable("bookId") Long bookId, HttpServletResponse resp, Model model) { public String bookDetail(@PathVariable("bookId") Long bookId, Model model) {
Book book = bookService.queryBookDetail(bookId); Book book = bookService.queryBookDetail(bookId);
if(book == null){
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return null;
}
model.addAttribute("book",book); model.addAttribute("book",book);
if(book.getLastIndexId() != null) { if(book.getLastIndexId() != null) {
//查询首章目录ID //查询首章目录ID
@ -121,12 +115,8 @@ public class PageController extends BaseController{
* */ * */
@SneakyThrows @SneakyThrows
@RequestMapping("/book/indexList-{bookId}.html") @RequestMapping("/book/indexList-{bookId}.html")
public String indexList(@PathVariable("bookId") Long bookId, HttpServletResponse resp, Model model) { public String indexList(@PathVariable("bookId") Long bookId, Model model) {
Book book = bookService.queryBookDetail(bookId); Book book = bookService.queryBookDetail(bookId);
if(book == null){
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return null;
}
model.addAttribute("book",book); model.addAttribute("book",book);
List<BookIndex> bookIndexList = bookService.queryIndexList(bookId,null,1,null); List<BookIndex> bookIndexList = bookService.queryIndexList(bookId,null,1,null);
model.addAttribute("bookIndexList",bookIndexList); model.addAttribute("bookIndexList",bookIndexList);
@ -139,15 +129,11 @@ public class PageController extends BaseController{
* */ * */
@SneakyThrows @SneakyThrows
@RequestMapping("/book/{bookId}/{bookIndexId}.html") @RequestMapping("/book/{bookId}/{bookIndexId}.html")
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, HttpServletResponse resp,Model model) { public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, Model model) {
//查询书籍 //查询书籍
Book book = bookService.queryBookDetail(bookId); Book book = bookService.queryBookDetail(bookId);
//查询目录 //查询目录
BookIndex bookIndex = bookService.queryBookIndex(bookIndexId); BookIndex bookIndex = bookService.queryBookIndex(bookIndexId);
if(book == null || bookIndex == null){
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return null;
}
model.addAttribute("book",book); model.addAttribute("book",book);
model.addAttribute("bookIndex",bookIndex); model.addAttribute("bookIndex",bookIndex);
//查询上一章节目录ID //查询上一章节目录ID