#1 -- Add custom error page
parent
e669750575
commit
de2125d251
@ -0,0 +1,40 @@
|
||||
package ru.ulstu.extractor.mvc;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@ControllerAdvice
|
||||
class GlobalDefaultExceptionHandler {
|
||||
public static final String DEFAULT_ERROR_VIEW = "error";
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
|
||||
// If the exception is annotated with @ResponseStatus rethrow it and let
|
||||
// the framework handle it - like the OrderNotFoundException example
|
||||
// at the start of this post.
|
||||
// AnnotationUtils is a Spring Framework utility class.
|
||||
/*if (AnnotationUtils.findAnnotation (e.getClass(), ResponseStatus.class) != null) {
|
||||
throw e;
|
||||
}
|
||||
*/
|
||||
|
||||
// Otherwise setup and send the user to a default error-view.
|
||||
ModelAndView mav = new ModelAndView();
|
||||
mav.addObject("exception", e);
|
||||
mav.addObject("url", req.getRequestURL());
|
||||
mav.setViewName(DEFAULT_ERROR_VIEW);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public String handle(NoHandlerFoundException ex) {
|
||||
return "DEFAULT_ERROR_VIEW";
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ru.ulstu.extractor.mvc;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class MvcConfiguration implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/{articlename:\\w+}");
|
||||
registry.addRedirectViewController("/", "/index");
|
||||
registry.addRedirectViewController("/default", "/home");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry
|
||||
.addResourceHandler("/webjars/**")
|
||||
.addResourceLocations("/webjars/");
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package ru.ulstu.extractor.mvc;
|
||||
|
||||
import nz.net.ultraq.thymeleaf.LayoutDialect;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
@Configuration
|
||||
public class TemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
||||
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.addTemplateResolver(templateResolver);
|
||||
templateEngine.addDialect(new LayoutDialect());
|
||||
return templateEngine;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>GitExtractor</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<script type="text/javascript" src="/webjars/jquery/3.6.0/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="/webjars/bootstrap/4.6.0/js/bootstrap.min.js"></script>
|
||||
<script type="text/javascript" src="/webjars/bootstrap-select/1.13.8/js/bootstrap-select.min.js"></script>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap-select/1.13.8/css/bootstrap-select.min.css"/>
|
||||
<link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light" layout:fragment="navbar">
|
||||
<a class="navbar-brand" href="#">Navbar</a>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav mr-auto">
|
||||
<li class="nav-item active">
|
||||
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#">Link</a>
|
||||
</li>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
|
||||
aria-haspopup="true" aria-expanded="false">
|
||||
Dropdown
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
||||
<a class="dropdown-item" href="#">Action</a>
|
||||
<a class="dropdown-item" href="#">Another action</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item" href="#">Something else here</a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="wrapper">
|
||||
<div id="sidebar" class="collapse navbar-collapse sidebar-mobile">
|
||||
<ul id="menu" class="list-unstyled">
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="content">
|
||||
<div id="breadcrumbs" class="container-fluid">
|
||||
</div>
|
||||
<div class="container-fluid">
|
||||
<ul id="messages" class="feedback-panel">
|
||||
</ul>
|
||||
</div>
|
||||
<div layout:fragment="content">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -1,53 +1,45 @@
|
||||
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
|
||||
<!--<head th:substituteby="header :: copy"></head>-->
|
||||
|
||||
<body>
|
||||
<h1>Support Friendly Error Page</h1>
|
||||
<div class="container" layout:fragment="content">
|
||||
<h1>Ошибка</h1>
|
||||
|
||||
<!-- As we are using Thymeleaf, you might consider using
|
||||
${#httpServletRequest.requestURL}. But that returns the path
|
||||
to this error page. Hence we explicitly add the url to the
|
||||
Model in some of the example code. -->
|
||||
<p th:if="${url}">
|
||||
<b>Page:</b> <span th:text="${url}">Page URL</span>
|
||||
<b>Страница:</b> <span th:text="${url}">Page URL</span>
|
||||
</p>
|
||||
|
||||
<p th:if="${timestamp}" id='created'>
|
||||
<b>Occurred:</b> <span th:text="${timestamp}">Timestamp</span>
|
||||
<b>Время:</b> <span th:text="${timestamp}">Timestamp</span>
|
||||
</p>
|
||||
|
||||
<p th:if="${status}">
|
||||
<b>Response Status:</b> <span th:text="${status}">status-code</span> <span
|
||||
th:if="${error}" th:text="'('+${error}+')'">error ...</span>
|
||||
</p>
|
||||
|
||||
<p th:if="${exception}">
|
||||
<b>Exception:</b> <span th:text="${exception}">exception</span> <span
|
||||
<p>
|
||||
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false"
|
||||
aria-controls="collapseExample">
|
||||
Показать stack trace
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<p>Application has encountered an error. Please contact support on
|
||||
...</p>
|
||||
|
||||
<p>Support may ask you to right click to view page source.</p>
|
||||
|
||||
<!--
|
||||
// Hidden Exception Details - this is not a recommendation, but here is
|
||||
// how you could hide an exception in the page using Thymeleaf
|
||||
-->
|
||||
<div th:utext="'<!--'" th:remove="tag"></div>
|
||||
<div class="collapse" id="collapseExample">
|
||||
<div class="card card-body">
|
||||
<div th:utext="'Failed URL: ' + ${url}" th:remove="tag">${url}</div>
|
||||
<div th:utext="'Exception: ' + ${exception.message}" th:remove="tag">${exception.message}</div>
|
||||
<ul th:remove="tag">
|
||||
<li th:each="ste : ${exception.stackTrace}" th:remove="tag"><span
|
||||
th:utext="${ste}" th:remove="tag">${ste}</span></li>
|
||||
</ul>
|
||||
<div th:utext="'-->'" th:remove="tag"></div>
|
||||
|
||||
|
||||
<div th:substituteby="footer :: copy"></div>
|
||||
|
||||
</body>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</html>
|
Loading…
Reference in New Issue