From e6697505757b0d3da83cab8525ceaacc9cd0e30d Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 16 Mar 2021 09:43:46 +0400 Subject: [PATCH] #1 -- Add simple mvc form --- build.gradle | 1 + .../extractor/mvc/GitExtractorController.java | 43 +++++++++++++++ .../ulstu/extractor/mvc/model/EmailForm.java | 40 ++++++++++++++ src/main/resources/templates/error.html | 53 +++++++++++++++++++ src/main/resources/templates/index.html | 30 +++++++++++ src/main/resources/templates/result.html | 25 +++++++++ 6 files changed, 192 insertions(+) create mode 100644 src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java create mode 100644 src/main/java/ru/ulstu/extractor/mvc/model/EmailForm.java create mode 100644 src/main/resources/templates/error.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/result.html diff --git a/build.gradle b/build.gradle index 227b6d5..cb55cf4 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,7 @@ 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: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' 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..6b02876 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java @@ -0,0 +1,43 @@ +package ru.ulstu.extractor.mvc; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.ModelAndView; +import ru.ulstu.extractor.mvc.model.EmailForm; + +import javax.servlet.http.HttpServletRequest; + +@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"; + } + + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public ModelAndView handleResourceNotFoundException(HttpServletRequest req, Exception ex) { + ModelAndView mav = new ModelAndView(); + mav.addObject("exception", ex); + mav.addObject("url", req.getRequestURL()); + mav.setViewName("error"); + return mav; + } +} 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/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..dd06612 --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,53 @@ + + + + + + +

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}
+ +
+ + +
+ + + \ 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..f37aa8f --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,30 @@ + + + + Простая обработка формы на Spring MVC + + + +

Форма

+
+

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