0% found this document useful (0 votes)
45 views25 pages

Spring MVC - Session 14 - Creating Custom Validation Rules

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views25 pages

Spring MVC - Session 14 - Creating Custom Validation Rules

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

SPRING MVC

21 July 2023

Creating custom validation rules– Session 7


2
Spring MVC Custom Validation

The Spring MVC framework allows us to perform custom


validations. In such case, we declare own annotations. We can
perform validation on the basis of own business logic.
Spring MVC Custom Validation Example
3

1. Add dependencies to [Link] file.

<!--[Link] spring-
webmvc -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-webmvc</artifactId>
<version>[Link]</version>
</dependency>
<!-- [Link] tomcat-
jasper -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.12</version>
</dependency>
4

<!
[Link]
api -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
5

<!-- [Link]
hibernate-validator -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-validator</artifactId>
<version>[Link]</version>
</dependency>
2. Create the bean class 6

[Link]

package [Link];

import [Link];
import [Link];
import [Link];

public class Employee {


private String name;
//Custom annotation
@Password
private String password;
//Predefined annotation
@Min(value=18, message="must be equal or greater than 18")
@Max(value=45, message="must be equal or less than 45")
private int age; 7
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
[Link] = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
}
3. Create the controller class 8

[Link]

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Controller
public class EmployeeController { 9
@RequestMapping("/hello")
public String showForm(Model theModel) {

[Link]("emp", new Employee());

return "viewpage";
}
@RequestMapping("/helloagain")
public String processForm(
@Valid @ModelAttribute("emp") Employee emp,
BindingResult br) {
if ([Link]()) {
return "viewpage";
}
else {
return "final";
}
}
}
4. Create the validator annotation 10

[Link]

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
11

@Constraint(validatedBy = [Link])
@Target( { [Link], [Link] } )
@Retention([Link])
public @interface Password {
//error message
public String message() default "must contain jtp";
//represents group of constraints
public Class<?>[] groups() default {};
//represents additional information about annotation
public Class<? extends Payload>[] payload() default {};
}
5. Create the validator class 12
[Link]
package [Link];

import [Link];
import [Link];

public class PasswordConstraintValidator implements ConstraintValidat


or<Password,String> {

public boolean isValid(String s, ConstraintValidatorContext cvc) {

boolean result=[Link]("jtp");
return result;
}
}
6. Provide the entry of controller in the [Link] file 13
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="[Link]
instance" xmlns="[Link] xsi:schemaLocation="http://
[Link]/xml/ns/javaee [Link]
app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>[Link]</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
7. Define the bean in the xml file 14
[Link]

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:context="[Link]
xmlns:mvc="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
<!-- Provide support for component scanning -->
<context:component-scan base-package="[Link]" />
15

<context:component-scan base-package="[Link]" />


<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean id="viewResolver" class="[Link]
[Link]">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
8. Create the requested page 16

[Link]

<html>
<body>
<a href="hello">Click here...</a>
</body>
</html>
9. Create the other view components 17
[Link]

<%@ taglib prefix="form" uri="[Link]


form" %>
<html>
<head>
<style>
.error {color:red}
</style>
</head>
<body>
<form:form action="helloagain" modelAttribute="emp">
Username: <form:input path="name" />
<br><br>

Password (*): <form:password path="password" />


<form:errors path="password" cssClass="error" />
<br><br>
18

Age (*): <form:input path="age" />


<form:errors path="age" cssClass="error" />
<br><br>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
19

[Link]

<%@ taglib uri="[Link] prefix="c" %>


<!DOCTYPE html>
<html>
<body>
Username: ${[Link]}<br><br>
Password: ${[Link]}<br><br>
Age: ${[Link]}
<br><br>
</body>
</html>
Output: 20

Output
21

Output
22

Output
23

Output
24

Output
25

Output

You might also like