From de2125d251429a316f4210a5d38759c5513b2918 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 16 Mar 2021 13:50:29 +0400 Subject: [PATCH] #1 -- Add custom error page --- build.gradle | 9 +- .../mvc/GlobalDefaultExceptionHandler.java | 40 +++++++++ .../ulstu/extractor/mvc/MvcConfiguration.java | 23 +++++ .../extractor/mvc/TemplateConfiguration.java | 19 ++++ src/main/resources/application.properties | 4 +- src/main/resources/templates/default.html | 67 +++++++++++++++ src/main/resources/templates/error.html | 86 +++++++++---------- src/main/resources/templates/index.html | 52 +++++------ 8 files changed, 225 insertions(+), 75 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java create mode 100644 src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java create mode 100644 src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java create mode 100644 src/main/resources/templates/default.html diff --git a/build.gradle b/build.gradle index cb55cf4..5bb687f 100644 --- a/build.gradle +++ b/build.gradle @@ -39,8 +39,6 @@ repositories { configurations { compile.exclude module: "spring-boot-starter-tomcat" - compile.exclude module: "jquery" - compile.exclude module: "popper.js" compile.exclude module: "follow-redirects" compile.exclude module: "is-buffer" } @@ -49,6 +47,7 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' + compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' @@ -57,6 +56,12 @@ dependencies { compile group: 'com.ibm.icu', name: 'icu4j', version: '63.1' compile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.9.0.202009080501-r' + compile group: 'org.webjars', name: 'jquery', version: '3.6.0' + compile group: 'org.webjars', name: 'bootstrap', version: '4.6.0' + compile group: 'org.webjars', name: 'bootstrap-select', version: '1.13.8' + compile group: 'org.webjars', name: 'font-awesome', version: '4.7.0' + + testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' } diff --git a/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java b/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java new file mode 100644 index 0000000..022764f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java @@ -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"; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java b/src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java new file mode 100644 index 0000000..1b55b43 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java @@ -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/"); + } +} diff --git a/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java b/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java new file mode 100644 index 0000000..4a0ad46 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java @@ -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; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c5d4c5d..c616bb3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,4 +2,6 @@ spring.main.banner-mode=off server.port=8080 # Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF logging.level.ru.ulstu=DEBUG -extractor.custom-projects-dir= \ No newline at end of file +extractor.custom-projects-dir= +# Thymeleaf Settings +spring.thymeleaf.cache=false diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..86a443c --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,67 @@ + + + + + GitExtractor + + + + + + + + + + +
+ + +
+ +
+ +
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index dd06612..bb0ca4a 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -1,53 +1,45 @@ + xmlns:th="http://www.thymeleaf.org" + xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" + layout:decorate="~{default}"> - -

Support Friendly Error Page

- - -

- Page: Page URL -

- -

- Occurred: Timestamp -

- -

- Response Status: status-code error ... -

- -

- Exception: exception - -

Application has encountered an error. Please contact support on - ...

- -

Support may ask you to right click to view page source.

- - -
-
${url}
-
${exception.message}
- -
- - -
- - +
+

Ошибка

+ + +

+ Страница: Page URL +

+ +

+ Время: Timestamp +

+ +

+ Response Status: status-code error ... +

+

+ +

+
+
+
${url}
+
${exception.message}
+
    +
  • ${ste}
  • +
+
+
+
\ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index f37aa8f..939171f 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,30 +1,32 @@ - - + + Простая обработка формы на Spring MVC - -

Форма

-
-

- - - - - - - - - - - -
Тема:
Кому:
Сообщение: