Thymleaf is not displaying the error messages returned from controller

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymleaf is not displaying the error messages returned from controller

stone
I'm new to thymleaf and I have designed one user login page for authentication as well as validations. But whenever user giving wrong information or empty fields  instead of displaying the error messages it's redirecting to the whitelabel error page.

Controller
------------
@Controller
public class HomeController {
       
        @Autowired
        private Validate validator;

        @RequestMapping(value="/",method=RequestMethod.GET)
        public String getHome(@ModelAttribute("userDetails")UserDetails details) {
                return "home";
        }
       
        @RequestMapping(value="/register",method=RequestMethod.POST)
        public String postData(@Valid @ModelAttribute("userDetails")UserDetails details, BindingResult errors, Model model) {
                if(validator.supports(UserDetails.class)) {
                        validator.validate(details, errors);
                        if(errors.hasErrors()) {
                                return "home";
                        }
                }
               
                return null;
        }
}

Validator.java
-----------------
@Component
public class Validate implements Validator {

        @Override
        public boolean supports(Class<?> clazz) {
               
                return clazz.isAssignableFrom(UserDetails.class);
        }

        @Override
        public void validate(Object command, Errors errors) {
                UserDetails cmd= null;
                cmd= (UserDetails)command;
               
                if(cmd.getUname().length()==0 || cmd.getUname().equalsIgnoreCase("")) {
                        errors.rejectValue("uname", "Empty.userDetails.uname");
                }
               
                if(cmd.getEmail().length()==0 && cmd.getEmail().equalsIgnoreCase("")) {
                        errors.rejectValue("email", "Empty.userDetails.email");
                }

        }

}

RootAppConfig.java
------------------------
@SpringBootApplication
public class ThymleafValidationApplication {

        public static void main(String[] args) {
                SpringApplication.run(ThymleafValidationApplication.class, args);
        }
       
        @Bean
        public MessageSource source() {
                ReloadableResourceBundleMessageSource source= new ReloadableResourceBundleMessageSource();
                source.setBasename("classpath:application");
                return source;
        }
}

MVCAppConfig.java
-----------------------
@Configuration
@ComponentScan(basePackages= {"com.st.controller","com.st.validator"})
public class MVCAppConfig {

}

application.properties
-------------------------
spring.thymeleaf.cache=false

Empty.userDetails.uname= Username Canot Be Empty
Empty.userDetails.email= Email Canot Be Empty

index.html
--------------
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Home | Page</title>
</head>
<body>
        <div align="center">
                <form th:action="@{/register}" th:object="${userDetails}" method="post">
                        <div>
                                <div>
                                        <label>Username</label>
                                        <div>
                                                <input type="text" th:field="*{uname}">
                                                ..
                                        </div>
                                </div>
                       
                                <div>
                                        <label>Email</label>
                                        <div>
                                                <input type="email" th:field="*{email}">
                                                ..
                                        </div>
                                </div>
                               
                                <div>
                                        <button>Login</button>
                                </div>
                        </div>
                </form>
        </div>
</body>
</html>

Errors
----------
1: 
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringErrorsTagProcessor' (template: "home" - line 15, col 51)
        at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        ... 62 common frames omitted
indicating to th:errors

2:
Caused by: org.springframework.context.NoSuchMessageException: No message found under code 'Empty.userDetails.uname.userDetails.uname' for locale 'en_US'.
        at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:91) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1276) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:725) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at org.springframework.web.servlet.support.BindStatus.initErrorMessages(BindStatus.java:308) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at org.springframework.web.servlet.support.BindStatus.getErrorMessages(BindStatus.java:278) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafBindStatus.getErrorMessages(SpringWebMvcThymeleafBindStatus.java:101) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.spring5.processor.SpringErrorsTagProcessor.doProcess(SpringErrorsTagProcessor.java:70) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        ... 75 common frames omitted
though the messages are defined on application.properties
Reply | Threaded
Open this post in threaded view
|

Re: Thymleaf is not displaying the error messages returned from controller

Reiju
Try to define your error messages in some "messages.properties" files instead of "application.properties".