0

Faced with such a problem that when using in pom.xml spring-boot-starter-validation stops displaying the html page with registration. Without this, Thymeleaf works correctly. I searched through all the forums, and did not find the problem.

RegistrationController


package ru.dnsbo.blogreferee2.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.dnsbo.blogreferee2.data.UsersRepository;
import ru.dnsbo.blogreferee2.form.RegistrationForm;
import ru.dnsbo.blogreferee2.services.RegistrationService;

import javax.validation.Valid;

@Controller
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private UsersRepository usersRepository;
    @Autowired
    RegistrationService registrationService;
    
    @GetMapping
    private String GetRegistrationPage() {
        return "registration";
    }
    
    @PostMapping
    private String PostRegistrationPage(Model model, @Valid RegistrationForm form,
                                        Errors errors) {
        //проверка на существующий email
        if(usersRepository.findByEmail(form.getEmail()).isPresent()) {
            return "registration";
        }
        //проверка на валидность
        if(errors.hasErrors()){
            return "registration";
        }
        registrationService.registration(form);
        return "redirect:/login";
    }

}

RegistrationForm


import lombok.Data;
import javax.validation.constraints.Size;

@Data
public class RegistrationForm {
@Size(min = 3,max = 10)
private String firstname;
private String lastname;
private String email;
private String numberPhone;
private String password;
private String country;
private String city;
private String dateBirthday;
private String street;
private String house;
private String flat;
}

RegistrationServiceImpl implements RegistrationService


@Service
@Builder
public class RegistrationServiceImpl implements RegistrationService {
@Autowired
private UsersRepository usersRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public void registration(RegistrationForm registrationForm) {
    
        String hashPassword = passwordEncoder.encode(registrationForm.getPassword());
    
        //заполняем форму для регистрации
        Users users = Users.builder()
                .firstname(registrationForm.getFirstname())
                .lastname(registrationForm.getLastname())
                .email(registrationForm.getEmail())
                .password(hashPassword)
                .numberPhone(registrationForm.getNumberPhone())
                .country(registrationForm.getCountry())
                .city(registrationForm.getCity())
                .dateBirthday(registrationForm.getDateBirthday())
                .street(registrationForm.getStreet())
                .house(registrationForm.getHouse())
                .flat(registrationForm.getFlat())
                //ставится по умолчанию
                .role(Role.USER)
                .state(State.ACTIVE)
                .build();
    
        //сохраняем пользователя в бд
        usersRepository.save(users);
    }

}

registration.html


<!DOCTYPE html>
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Московская Федерация Судей</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link th:href="@{/css/registrationAndLogin}" rel="stylesheet" type="text/css">
    <script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script src="https://unpkg.com/flowbite@1.5.5/dist/datepicker.js"></script>


    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        clifford: '#da373d',
                    }
                }
            }
        }
    </script>


</head>
<body>
<header th:insert="blocks/header :: header"></header>
<main>
    <div class="hidden sm:block" aria-hidden="true">
        <div class="py-5">
            <div class="border-t border-gray-200"></div>
        </div>
    </div>

    <div class="mt-10 sm:mt-0">
        <div class="md:grid md:grid-cols-4   md:gap-6">
            <div class="mt-5 md:col-start-2 md:col-span-2 md:mt-0">
                <!-- в метод registration post отравляется запрос-->
                <form th:action="@{/registration}" th:object="${form}" method="post">
                    <div class="overflow-hidden shadow sm:rounded-md border-2 border-white">
                        <div class="bg-sky-50 px-4 py-5 sm:p-6">
                            <div class="grid grid-cols-6 gap-6">

                                <!--Поле ввода для имени-->

                                <div class="col-span-6 sm:col-span-3">
                                    <label for="firstname" class="block text-sm
                                     font-medium text-gray-700">

                                        Имя</label>
                                    <input type="text" name="firstname" id="firstname"
                                           autocomplete="firstname" th:field="*{firstname}"
                                           class=" mt-1 block w-full rounded-md border-gray-300
                                            shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
                                          />
                                    <div th:if="${#fields.hasErrors('firstname')}"
                                         th:errors="*{firstname}" >
                                        Ошибка ввода
                                    </div>
                                </div>

                        //прочий код

                        <!--Кнопка сохранить и отравить в бд-->

                        <div class="bg-indigo-50 px-4 py-3 text-right sm:px-6">
                            <button type="submit"
                                    class="inline-flex justify-center rounded-md border border-transparent
                                     bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700
                                     focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
                                Сохранить
                            </button>
                        </div>

                    </div>
                </form>
            </div>
        </div>
    </div>
</main>
</body>
</html>

Mistakes:


2022-12-14 17:41:07.634 ERROR 12580 --- \[p-nio-80-exec-1\] org.thymeleaf.TemplateEngine             : \[THYMELEAF\]\[http-nio-80-exec-1\] Exception processing template "registration": An error happened during template parsing (template: "class path resource \[templates/registration.html\]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource \[templates/registration.html\]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) \~\[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) \~\[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) \~\[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) \~\[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) \~\[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366) \~\[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190) \~\[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE\]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1405) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1149) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1088) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:670) \~\[tomcat-embed-core-9.0.68.jar:4.0.FR\]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) \~\[spring-webmvc-5.3.23.jar:5.3.23\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779) \~\[tomcat-embed-core-9.0.68.jar:4.0.FR\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) \~\[tomcat-embed-websocket-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:337) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:81) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:122) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:116) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:109) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:223) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:112) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:82) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.session.DisableEncodeUrlFilter.doFilterInternal(DisableEncodeUrlFilter.java:42) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:221) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:186) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]

i know there is something terribly wrong with my understanding of how this whole annotation thing gets wired up and maps to the Valid. I've rummaged through all combinations and posts in StOv for similar exceptions, but I've clearly missed something. could someone be so kind as to point out my blunder here?

I could make validation work through js or html, but it is important for me that it is implemented through Java

advance thanks

Alice
  • 1
  • 1
  • If that is really your HTML the template is just wrong, what is with all those `\` in front of each line? – M. Deinum Dec 15 '22 at 09:33
  • Yes, this is my html code It can be seen when I copied from another forum, it was substituted by itself. Fixed it, but it's not the original error. Thank you for noticing. – Alice Dec 15 '22 at 12:06
  • Please include the full error as that error will tell you which line has the error. However due to you copy pasting from different places additional cruft has been added to the texts (just as with your HTML) making it pretty hard to read. Nonetheless this isn't the full stacktrace I suspect as the original one would have a more descripive error. – M. Deinum Dec 15 '22 at 12:25

0 Answers0