#1 -- Add simple mvc form

This commit is contained in:
Anton Romanov 2021-03-16 09:43:46 +04:00
parent 0bf6947060
commit e669750575
6 changed files with 192 additions and 0 deletions

View File

@ -48,6 +48,7 @@ configurations {
dependencies { dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' 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-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.module', name: 'jackson-module-afterburner'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'

View File

@ -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;
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -0,0 +1,53 @@
<!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">
<!--<head th:substituteby="header :: copy"></head>-->
<body>
<h1>Support Friendly Error Page</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>
</p>
<p th:if="${timestamp}" id='created'>
<b>Occurred:</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>
<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="'&lt;!--'" th:remove="tag"></div>
<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="'--&gt;'" th:remove="tag"></div>
<div th:substituteby="footer :: copy"></div>
</body>
</html>

View File

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Форма</h1>
<form action="#" th:action="@{/sendEmail}" th:object="${emailForm}" method="post">
<p style="color:red" th:text="${error}"></p>
<table>
<tr>
<td>Тема:</td>
<td><input type="text" th:field="*{subject}"/></td>
</tr>
<tr>
<td>Кому:</td>
<td><input type="text" th:field="*{to}"/></td>
</tr>
<tr>
<td>Сообщение:</td>
<td><textarea th:field="*{message}"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Отправить"/></td>
</tr>
</table>
</form>
</body>
</html>

View File

@ -0,0 +1,25 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Результат обработки формы</h1>
<table>
<tr>
<td>Тема:</td>
<td><p th:text="${emailForm.subject}"/></td>
</tr>
<tr>
<td>Кому:</td>
<td><p th:text="${emailForm.to}"/></td>
</tr>
<tr>
<td>Сообщение:</td>
<td><p th:text="${emailForm.message}"/></td>
</tr>
</table>
<a href="/">Отправить другое сообщение</a>
</body>
</html>