diff --git a/build.gradle b/build.gradle index cd5160e..26c609b 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" } @@ -48,6 +46,8 @@ configurations { 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' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc' @@ -58,6 +58,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/GitExtractorController.java b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java new file mode 100644 index 0000000..487479c --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java @@ -0,0 +1,26 @@ +package ru.ulstu.extractor.mvc; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import ru.ulstu.extractor.mvc.model.EmailForm; + +@Controller +public class GitExtractorController { + @GetMapping("/") + public String indexForm(Model model) { + model.addAttribute("emailForm", new EmailForm()); + return "index"; + } + + @PostMapping("/sendEmail") + public String sendEmail(@ModelAttribute EmailForm emailForm, Model model) { + if (emailForm.getTo().isEmpty()) { + model.addAttribute("error", "'Кому' не должно быть пустым"); + return "index"; + } + return "result"; + } +} 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..7a1ea5b --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java @@ -0,0 +1,30 @@ +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) { + 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..7798224 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java @@ -0,0 +1,33 @@ +package ru.ulstu.extractor.mvc; + +import nz.net.ultraq.thymeleaf.LayoutDialect; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ReloadableResourceBundleMessageSource; +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()); + templateEngine.setMessageSource(messageSource()); + return templateEngine; + } + + @Value("${messages.basename.path}") + private String messagesBasename; + + public MessageSource messageSource() { + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setFallbackToSystemLocale(false); + messageSource.setBasenames("file:" + messagesBasename); + return messageSource; + } +} diff --git a/src/main/java/ru/ulstu/extractor/mvc/model/EmailForm.java b/src/main/java/ru/ulstu/extractor/mvc/model/EmailForm.java new file mode 100644 index 0000000..59765ee --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/model/EmailForm.java @@ -0,0 +1,40 @@ +package ru.ulstu.extractor.mvc.model; + +public class EmailForm { + private String subject; + private String to; + private String message; + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "EmailForm{" + + "subject='" + subject + '\'' + + ", to='" + to + '\'' + + ", message='" + message + '\'' + + '}'; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7b5190f..734980c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,6 +3,9 @@ server.port=8080 # Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF logging.level.ru.ulstu=DEBUG extractor.custom-projects-dir= +# Thymeleaf Settings +spring.thymeleaf.cache=false +messages.basename.path=messages_en.properties # JPA Settings spring.datasource.url=jdbc:postgresql://localhost:5432/repo spring.datasource.username=postgres diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties new file mode 100644 index 0000000..34bbcb8 --- /dev/null +++ b/src/main/resources/messages.properties @@ -0,0 +1,4 @@ +messages.app-name=GitExtractor v0.1.0 +messages.menu.home=Main +messages.menu.indexed-repos=List of indexed repos +messages.menu.new-repo=Analyse new repo \ No newline at end of file diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties new file mode 100644 index 0000000..34bbcb8 --- /dev/null +++ b/src/main/resources/messages_en.properties @@ -0,0 +1,4 @@ +messages.app-name=GitExtractor v0.1.0 +messages.menu.home=Main +messages.menu.indexed-repos=List of indexed repos +messages.menu.new-repo=Analyse new repo \ No newline at end of file diff --git a/src/main/resources/messages_ru.properties b/src/main/resources/messages_ru.properties new file mode 100644 index 0000000..dfda9c7 --- /dev/null +++ b/src/main/resources/messages_ru.properties @@ -0,0 +1,4 @@ +messages.app-name=GitЁxtractor v0.1.0 +messages.menu.home=На главную +messages.menu.indexed-repos=Список проиндексированных репозиториев +messages.menu.new-repo=Анализ нового репозитория \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..98fbb34 --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,55 @@ + + + + + app-name + + + + + + + + + + +
+ + +
+ +
+ +
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..bb0ca4a --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,45 @@ + + + + + +
+

Ошибка

+ + +

+ Страница: 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 new file mode 100644 index 0000000..939171f --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,32 @@ + + + + Простая обработка формы на Spring MVC + + +
+

Форма

+
+

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