分别渲染404和500页面

pull/137/head
ruibaby 2019-03-17 09:25:38 +08:00
parent 4af8bd11d0
commit 313e6ec437
3 changed files with 54 additions and 23 deletions

View File

@ -7,7 +7,7 @@ import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.LocaleMessageUtil; import cc.ryanc.halo.utils.LocaleMessageUtil;
import cc.ryanc.halo.utils.ThemeUtils; import cc.ryanc.halo.utils.ThemeUtils;
import cc.ryanc.halo.web.controller.admin.base.BaseController; import cc.ryanc.halo.web.controller.admin.base.BaseController;
import cc.ryanc.halo.web.controller.core.BaseFrontController; import cc.ryanc.halo.web.controller.core.BaseContentController;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader; import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.io.file.FileWriter;
@ -58,7 +58,7 @@ public class ThemeController extends BaseController {
*/ */
@GetMapping @GetMapping
public String themes(Model model) { public String themes(Model model) {
model.addAttribute("activeTheme", BaseFrontController.THEME); model.addAttribute("activeTheme", BaseContentController.THEME);
model.addAttribute("themes", THEMES); model.addAttribute("themes", THEMES);
return "admin/admin_theme"; return "admin/admin_theme";
} }
@ -77,7 +77,7 @@ public class ThemeController extends BaseController {
HttpServletRequest request) { HttpServletRequest request) {
try { try {
optionService.saveOption("theme", themeName); optionService.saveOption("theme", themeName);
BaseFrontController.THEME = themeName; BaseContentController.THEME = themeName;
configuration.setSharedVariable("themeName", themeName); configuration.setSharedVariable("themeName", themeName);
log.info("Changed theme to {}", themeName); log.info("Changed theme to {}", themeName);
return new JsonResult(1, localeMessage("code.admin.theme.change-success", new Object[]{themeName})); return new JsonResult(1, localeMessage("code.admin.theme.change-success", new Object[]{themeName}));
@ -228,7 +228,7 @@ public class ThemeController extends BaseController {
*/ */
@GetMapping(value = "/editor") @GetMapping(value = "/editor")
public String editor(Model model) { public String editor(Model model) {
final List<String> templates = ThemeUtils.getTplName(BaseFrontController.THEME); final List<String> templates = ThemeUtils.getTplName(BaseContentController.THEME);
model.addAttribute("tpls", templates); model.addAttribute("tpls", templates);
return "admin/admin_theme-editor"; return "admin/admin_theme-editor";
} }
@ -245,7 +245,7 @@ public class ThemeController extends BaseController {
String tplContent = ""; String tplContent = "";
try { try {
final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath()); final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath());
themePath.append(BaseFrontController.THEME); themePath.append(BaseContentController.THEME);
themePath.append("/"); themePath.append("/");
themePath.append(tplName); themePath.append(tplName);
final File themesPath = new File(themePath.toString()); final File themesPath = new File(themePath.toString());
@ -273,7 +273,7 @@ public class ThemeController extends BaseController {
} }
try { try {
final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath()); final StrBuilder themePath = new StrBuilder(ThemeUtils.getUsersThemesPath().getAbsolutePath());
themePath.append(BaseFrontController.THEME); themePath.append(BaseContentController.THEME);
themePath.append("/"); themePath.append("/");
themePath.append(tplName); themePath.append(tplName);
final File tplPath = new File(themePath.toString()); final File tplPath = new File(themePath.toString());

View File

@ -11,7 +11,7 @@ import cn.hutool.core.text.StrBuilder;
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2017/12/15 * @date : 2017/12/15
*/ */
public abstract class BaseFrontController { public abstract class BaseContentController {
/** /**
* *

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.web.controller.core; package cc.ryanc.halo.web.controller.core;
import cc.ryanc.halo.logging.Logger; import cc.ryanc.halo.logging.Logger;
import cn.hutool.core.text.StrBuilder;
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -20,6 +21,7 @@ import javax.servlet.http.HttpServletRequest;
public class CommonController implements ErrorController { public class CommonController implements ErrorController {
private static final String ERROR_PATH = "/error"; private static final String ERROR_PATH = "/error";
private final Logger log = Logger.getLogger(getClass()); private final Logger log = Logger.getLogger(getClass());
/** /**
@ -32,6 +34,8 @@ public class CommonController implements ErrorController {
public String handleError(HttpServletRequest request) { public String handleError(HttpServletRequest request) {
final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
final String requestURI = request.getRequestURI();
log.error("Error path: [{}], status: [{}]", request.getRequestURI(), statusCode); log.error("Error path: [{}], status: [{}]", request.getRequestURI(), statusCode);
// Get the exception // Get the exception
@ -43,37 +47,64 @@ public class CommonController implements ErrorController {
if (StringUtils.startsWithIgnoreCase(throwable.getMessage(), "Could not resolve view with name '")) { if (StringUtils.startsWithIgnoreCase(throwable.getMessage(), "Could not resolve view with name '")) {
// TODO May cause unreasoned problem // TODO May cause unreasoned problem
// if Ftl was not found then redirect to /404 // if Ftl was not found then redirect to /404
return "redirect:/404"; if (requestURI.contains("/admin")) {
return "redirect:/admin/404";
} else {
return "redirect:/404";
}
} }
} }
if (requestURI.contains("/admin")) {
// if (statusCode.equals(CommonParamsEnum.NOT_FOUND.getValue())) { return "redirect:/admin/500";
// return "redirect:/404"; } else {
// } else { return "redirect:/500";
// return "redirect:/500"; }
// }
// TODO Complete error handler
return "redirect:/500";
} }
/** /**
* 404 * Render 404 error page
* *
* @return String * @return template path:
*/ */
@GetMapping(value = "/404") @GetMapping(value = "/admin/404")
public String fourZeroFour() { public String adminNotFround() {
return "common/error/404"; return "common/error/404";
} }
/** /**
* 500 * Render 500 error page
*
* @return template path:
*/
@GetMapping(value = "/admin/500")
public String adminInternalError() {
return "common/error/500";
}
/**
* Render 404 error page
* *
* @return String * @return String
*/ */
@GetMapping(value = "/404")
public String contentNotFround() {
StrBuilder path = new StrBuilder("themes/");
path.append(BaseContentController.THEME);
path.append("/404");
return path.toString();
}
/**
* Render 500 error page
*
* @return template path:
*/
@GetMapping(value = "/500") @GetMapping(value = "/500")
public String fiveZeroZero() { public String contentInternalError() {
return "common/error/500"; StrBuilder path = new StrBuilder("themes/");
path.append(BaseContentController.THEME);
path.append("/500");
return path.toString();
} }
/** /**