|
|
|
@ -4,12 +4,9 @@ import com.fasterxml.jackson.core.TreeNode;
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
@ -33,7 +30,6 @@ import ${domain}.frame.utils.AESUtil;
|
|
|
|
|
import ${domain}.frame.utils.LogUtil;
|
|
|
|
|
import ${domain}.frame.utils.MD5Util;
|
|
|
|
|
import ${domain}.frame.utils.MapperUtil;
|
|
|
|
|
import ${domain}.frame.utils.RequestUtil;
|
|
|
|
|
import ${domain}.frame.utils.StringUtil;
|
|
|
|
|
<#if project.frameValue() = 1>
|
|
|
|
|
import ${domain}.module.wsys.mgr.TokensManager;
|
|
|
|
@ -58,7 +54,6 @@ import java.util.regex.Pattern;
|
|
|
|
|
* 全局htm后缀入口{@link GlobalController#action(Model, HttpServletRequest, HttpServletResponse)}
|
|
|
|
|
* 全局ajax入口{@link GlobalController#ajax(String, String, String, HttpServletRequest, HttpServletResponse, String, MultipartFile)}
|
|
|
|
|
* 全局消息订阅{@link GlobalController#sse(String)}
|
|
|
|
|
* 全局异常捕捉{@link GlobalController#exceptionHandler(HttpServletRequest, HttpServletResponse, Model, Exception)}
|
|
|
|
|
* <p>
|
|
|
|
|
* 说明Request命名规则,驼峰式命名
|
|
|
|
|
* Api#Example#Request ==> 目标#动作#Request
|
|
|
|
@ -68,8 +63,7 @@ import java.util.regex.Pattern;
|
|
|
|
|
* @since 2017-01-01
|
|
|
|
|
*/
|
|
|
|
|
@Controller
|
|
|
|
|
@ControllerAdvice
|
|
|
|
|
public class GlobalController implements ErrorController {
|
|
|
|
|
public class GlobalController {
|
|
|
|
|
|
|
|
|
|
@Value("${r'${server.servlet.context-path}'}")
|
|
|
|
|
private String context;
|
|
|
|
@ -80,65 +74,6 @@ public class GlobalController implements ErrorController {
|
|
|
|
|
@Autowired
|
|
|
|
|
private FreeMarkerViewResolver viewResolver;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全局异常捕捉
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
* @param response
|
|
|
|
|
* @param exception 要捕获的异常
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
|
|
public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Model model, Exception exception) {
|
|
|
|
|
StringBuffer msg = new StringBuffer("");
|
|
|
|
|
if (exception != null) {
|
|
|
|
|
msg = new StringBuffer("");
|
|
|
|
|
String message = exception.toString();
|
|
|
|
|
int length = exception.getStackTrace().length;
|
|
|
|
|
if (length > 0) {
|
|
|
|
|
msg.append("<a>").append(message).append("</a><br>");
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
msg.append("<a>").append(exception.getStackTrace()[i]).append("</a><br>");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
msg.append(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
model.addAttribute("msg", msg.toString());
|
|
|
|
|
return "500";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final static String ERROR_PATH = "/error";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getErrorPath() {
|
|
|
|
|
return ERROR_PATH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = ERROR_PATH)
|
|
|
|
|
public String error(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
switch (response.getStatus()) {
|
|
|
|
|
case 404:
|
|
|
|
|
return "404";
|
|
|
|
|
case 403:
|
|
|
|
|
String errorUrl = RequestUtil.getErrorUrl(request);
|
|
|
|
|
errorUrl = errorUrl.replaceFirst(context, "");
|
|
|
|
|
if ((errorUrl.equals(homePage) || errorUrl.equals("/")) && LocalData.getToken() == null) {
|
|
|
|
|
try {
|
|
|
|
|
response.sendRedirect(context + loginPage);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "403";
|
|
|
|
|
case 500:
|
|
|
|
|
return "500";
|
|
|
|
|
default:
|
|
|
|
|
return "403";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/")
|
|
|
|
|
public String home() {
|
|
|
|
|
Token token = LocalData.getToken();
|
|
|
|
@ -230,7 +165,7 @@ public class GlobalController implements ErrorController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return exceptionHandler(request, response, model, e);
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo 可在此获取共性数据(也可以在全局拦截器GlobalHandlerInterceptor、拦截器作用域比此更高),
|
|
|
|
|