In the SpringBoot project, I validate the form, the get request receives data verification and an error is immediately written.

Controller

@Controller @RequestMapping("registration") public class RegistrationController extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("results").setViewName("results"); } @RequestMapping(method = RequestMethod.GET) public String init(RegistrationForm registrationForm) { System.out.println("get"); return "form"; } @RequestMapping(method = RequestMethod.POST) public String checkRegistratioInfo(@Valid RegistrationForm registrationForm, BindingResult bindingResult) { System.out.println("post"); if (bindingResult.hasErrors()) { return "form"; } return "redirect:results"; } 

form, there are heteras, setters.

  public class RegistrationForm { @NotNull(message = "Please enter your login.") @Size(min = 3, max = 20, message = "Your login must between 3 and 20 characters") private String login; @NotNull(message = "Please enter your password.") @Size(min = 5, max = 20, message = "Your password must between 5 and 20 characters") private String password; @NotNull(message = "Please enter your email first name.") @Size(min = 4, max = 20, message = "Your first name must between 4 and 20 characters") private String first_name; @NotNull(message = "Please enter your email last name.") @Size(min = 4, max = 20, message = "Your last name must between 4 and 20 characters") private String last_name; @NotNull(message = "Please enter your email middle name.") @Size(min = 4, max = 20, message = "Your middle name must between 4 and 20 characters") private String middle_name; @NotNull(message = "Please enter your information about yourself") @Size(min = 20, message = "Your information about yourself must be min 20 characters") private String your_info; @NotNull(message = "Please enter your mobile phone.") private Integer mobile_phone; private Integer home_phone; @Size(min = 10, message = "Your address must be min 10 characters") private String address; @Email private String email; 

html page

 <html xmlns:th="http://www.w3.org/1999/xhtml"> <body> <form action="#" th:th:action="@{registration}" th:object="${registrationForm}" method="post"> <table> <tr> <tr class="tg"> <th colspan="2"><h4>Регистрация</h4></th> <tr> <tr> <td>Login:</td> <td><input type="text" th:field="*{login}"/></td> <td th:if="${#fields.hasErrors('login')}" th:errors="*{login}">Name Error</td> </tr> <tr> <td>Пароль:</td> <td><input type="text" th:field="*{password}"/></td> <td th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Name Error</td> </tr> <tr> <td>Имя:</td> <td><input type="text" th:field="*{first_name}"/></td> <td th:if="${#fields.hasErrors('first_name')}" th:errors="*{first_name}">Name Error</td> </tr> <tr> <td>Фамилия:</td> <td><input type="text" th:field="*{last_name}"/></td> <td th:if="${#fields.hasErrors('last_name')}" th:errors="*{last_name}">Name Error</td> </tr> <tr> <td>Отчество:</td> <td><input type="text" th:field="*{middle_name}"/></td> <td th:if="${#fields.hasErrors('middle_name')}" th:errors="*{middle_name}">Name Error</td> </tr> <tr> <td>Информация о Вас:</td> <td><input type="text" th:field="*{your_info}"/></td> <td th:if="${#fields.hasErrors('your_info')}" th:errors="*{your_info}">Name Error</td> </tr> <tr> <td>Мобильный телефон:</td> <td><input type="text" th:field="*{mobile_phone}"/></td> <td th:if="${#fields.hasErrors('mobile_phone')}" th:errors="*{mobile_phone}">Name Error</td> </tr> <tr> <td>Домашний телефон:</td> <td><input type="text" th:field="*{home_phone}"/></td> <td th:if="${#fields.hasErrors('home_phone')}" th:errors="*{home_phone}">Name Error</td> </tr> <tr> <td>Адрес:</td> <td><input type="text" th:field="*{address}"/></td> <td th:if="${#fields.hasErrors('address')}" th:errors="*{address}">Name Error</td> </tr> <tr> <td>Email:</td> <td><input type="email" th:field="*{email}"/></td> <td th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name Error</td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Регистрация"></td> </tr> </table> </form> </body> </html> 

Tell me what needs to be changed so that on the gett the request was a normal page display, did the data check on the post? reference to the project

    1 answer 1

    The problem is that Spring MVC is configured to use JSP pages as a View, but the jsp itself is written partly in the syntax of the Thymeleaf templates. To solve the problem, there are two options:
    Option 1:
    Rewrite the one described in the Thymeleaf template on JSP using tags provided by Spring MVC, so (part of the form is missing):

     <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@include file="head.jsp"%> <!DOCTYPE html> <html> <head> <title>Register Page</title> <meta charset="utf-8"> </head> <body> <center> <div class="box"> <form:form method="post" modelAttribute="registrationForm"> <table> <tr> <tr class="tg"> <th colspan="2"><h4>Регистрация</h4></th> <tr> <tr> <td>Login:</td> <td><form:input path="login" type="text" /></td> <td><form:errors path="login" /></td> </tr> <tr> <td>Пароль:</td> <td><form:input path="password" type="text" /></td> <td><form:errors path="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Регистрация"></td> </tr> </table> </form:form> </div> </center> </body> </html> 

    In the RegistrationController class, you need to change the init method to:

     @RequestMapping(method = RequestMethod.GET) public String init(ModelMap modelMap) { modelMap.put("registrationForm", new RegistrationForm()); return "Register"; } 

    Option 2:
    Use Thymeleaf as a View in Spring MVC.
    To do this, you need to remove the viewResolver method from the AppConfig viewResolver and fully viewResolver existing pages to Thymeleaf format. An example can be found here .

    A little comment on the processing of the POST method
    When processing the POST method, you should adhere to the Post / Redirect / Get paradigm, this will avoid problems with re-sending the form (for example, if the user after a successful save updates the page in the browser). To do this, the RegistrationController#checkRegistratioInfo should be rewritten as follows:

     @RequestMapping(method = RequestMethod.POST) public String checkRegistratioInfo( @Valid @ModelAttribute("registrationForm") RegistrationForm registrationForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "Register"; } return "redirect:welcome"; } 

    And in HomeController add method

     @RequestMapping(path="welcome", method = RequestMethod.GET) public String welcome(final HttpServletRequest request, final HttpServletResponse response) { return "Welcome"; }