Spring MVC validation not working. How to do?

Can't do validation in spring mvc. Two days I and without to no avail.

This is my whole project
Image IDE

5e9812da85181385157407.png


Here is my user
User

package com.springfreemarkerform;


import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class User {


@NotNull
@NotEmpty
 @Size(min = 1, max = 2)
 private String username;



 public String getUsername() {
 return username;
}

 public void setUsername(String username) {
 this.username = username;
}

}



And this is my my controller
FormController

package com.springfreemarkerform;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;

@Controller
public class FormController {


@GetMapping("/")
 public String home() {
 return "redirect:/form";
}




@GetMapping("/form")
 public String getForm (Model model) {

 model.addAttribute("user", new User());
 return "/form";

}




@PostMapping("/form")
 public String postForm(@Valid User User, BindingResult result) {

 if (result.hasErrors()) {
 return "/error";
}
 return "good";
}


}



And here is the HTML page
form

<!DOCTYPE html>
<html lang="en">
the <head>
 <meta charset="UTF-8">
<title>Form</title>
</head>
the <body>


<form action="/form" method="POST" name="user">

 <input type="text" name="username" >
 <button type="submit">submit</button>

</form>


</body>
</html>



And this pom.xml
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.springfreemarkerform</groupId>
<artifactId>SpringFreemarkerForm</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${encoding}</encoding>
</configuration>
</plugin>

</plugins>
</build>

the <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<java.version>1.8</java.version>
<encoding>UTF-8</encoding>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>



<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.2.Final</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>

</project>



Further tuning of the java classes, since I'm not using xml configuration.

It ServletInit
ServletInit

package com.springfreemarkerform;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherservletinitializer;

public class ServletInitSakh extends AbstractAnnotationConfigDispatcherservletinitializer {

@Override
 protected Class<?>[] getRootConfigClasses() {
 return null;
}

@Override
 protected Class<?>[] getServletConfigClasses() {
 return new Class<?>[] { FreeMarkerConfigSakh.class };
}

@Override
 protected String[] getServletMappings() {
 return new String[] { "/" };
}
}



And it FreeMarkerConfig
FreeMarkerConfig

package com.springfreemarkerform;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Properties;

@Configuration
@ComponentScan("com.springfreemarkerform")
public class FreeMarkerConfigSakh {

@Bean
 public FreeMarkerConfigurer freeMarkerConfigurer() {
 FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/templates");
configurer.setDefaultEncoding("UTF-8");
 configurer.setFreemarkerSettings(new Properties() {{
 put("default_encoding", "UTF-8");
}});
 return configurer;
}

@Bean
 getViewResolver public ViewResolver(){
 FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setSuffix(".ftl");
viewResolver.setContentType("text/html;charset=UTF-8");
viewResolver.setCache(false);
 return viewResolver;
}
}



As I understand it, if I specify in the input is not a valid value, well, for example, leave blank or indicate more than two signs and sends this form is meant to happen is the miracle, that is, the value from the form, namely the field surname, should not enter into the field surname, which is in the class User. And BindingResult which is in the controller in method postForm, I must inform you that that is not so. But this BindingResult silent as a fish, and because of this, I have nothing works.
That is, briefly and simply. If I specify a not valid value, I always returns the exact same page good and should return error if not valid data.
April 19th 20 at 12:17
1 answer
April 19th 20 at 12:19
Solution
The issue has been resolved. You need to specify RootConfig @EnableWebMvc thereby activate WebMvc

Find more questions by tags ValidationSpring