educative.io

Educative

@valid annotation doesnt work

I was referring to this section link

  1. I have defeined the Athlete model with lastName as mandatory.

  2. I have also defined AthleteController as follows with @valid annotation associated with

     public String processPlayerForm(@ModelAttribute("athlete") @Valid Athlete athlete,
     		BindingResult result) {
     	if (result.hasErrors()) {
     		return "add-player-form";
     	}
     	return "player-confirmation";
     }
    
  3. I have created the form where the last name can be inputed from user.

     <form:form action="processPlayerForm" modelAttribute="athlete">
     	Last Name *: <form:input path="lastName"/>
     	         	  <form:errors path="lastName" cssClass="error"/><br>
    

When empty values are given in the form and submitted, annotation doesnt throw any error. Can someone pls help me with what mistake am i doing?

pom.xml - dependencies:

	<dependency>
		<groupId>javax</groupId>
		<artifactId>javaee-web-api</artifactId>
		<version>6.0</version>
		<scope>provided</scope>
	</dependency>
	
	<!-- For spring MVC -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>5.2.12.RELEASE</version>
	</dependency>

player-servlet.xml:

    <context:component-scan base-package="io.learn.springmvc" />

    <mvc:annotation-driven />