Forms Validation in Spring MVC Framework

This project will demonstrate how to use Forms Validation in Spring MVC Framework

  • SpringMVCFramework
    • src/main/java
      • com.demo
        • SpringMVCFramework.java
      • com.demo.configurations
        • ValidationConfiguration.java
      • com.demo.controllers
        • AccountController.java
      • com.demo.entities
        • Account.java
      • com.demo.validators
        • AccountValidator.java
    • src/main/resources
      • static
      • templates
      • application.properties
      • messages.properties
    • src
      • main
        • webapp
          • WEB-INF
            • views
              • account
                • index.jsp
                • success.jsp
    • pom.xml
package com.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringMVCFramework {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringMVCFramework.class, args);
        }
    }
package com.demo.controllers;

import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo.entities.Account;
import com.demo.validators.AccountValidator;

@Controller
@RequestMapping(value = "account")
public class AccountController {

    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        modelMap.put("account", new Account());
        return "account/index";
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(@ModelAttribute(value = "account") @Valid Account account, BindingResult bindingResult, ModelMap modelMap) {
        AccountValidator accountValidator = new AccountValidator();
        accountValidator.validate(account, bindingResult);
        if (bindingResult.hasErrors()) {
            return "account/index";
        } else {
            modelMap.put("account", account);
            return "account/success";
        }
    }

}
package com.demo.configurations;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class SpringMVCConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setBasename("messages");
        return resourceBundleMessageSource;
    }

}
package com.demo.entities;

import java.util.Date;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.URL;
import org.springframework.format.annotation.DateTimeFormat;

public class Account {

    @NotEmpty
    @Length(min = 3, max = 10)
    private String username;

    @NotEmpty
    @Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})")
    private String password;

    @NotEmpty
    @Email
    private String email;

    @NotNull
    @Min(18)
    @Max(120)
    private int age;

    @URL
    private String website;

    @DateTimeFormat(pattern = "MM/dd/yyyy")
    @NotNull
    @Past
    private Date birthDay;

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

}
package com.demo.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.demo.entities.Account;

public class AccountValidator implements Validator {

    @Override
    public boolean supports(Class arg0) {
        return Account.class.equals(arg0);
    }

    @Override
    public void validate(Object object, Errors errors) {
    }

}
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**

server.port=9596
NotEmpty=This field is required.
Length=Please enter a value between {2} and {1} characters long.
Min=Please enter a value greater than or equal to {1}.
Max=Please enter a value less than or equal to {1}.
NotNull=This field is required.
account.username.exists=Username already exists.
typeMismatch.int=Please enter a valid number.
URL.website=Please enter a valid URL.
Email=Please enter a valid email address.
StrongPassword=Password does not meet complexity requirements.
typeMismatch={0} is of invalid format.
Pattern={0} must match pattern for complexity requirements.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Account Register</title>
<style type="text/css">
.error {
    color: red;
}
</style>
</head>
<body>

    <h3>Account Register</h3>
    <s:form method="post" modelAttribute="account"
        action="${pageContext.request.contextPath }/account/save">
        <table cellpadding="2" cellspacing="2" border="0">

            <tr valign="top">
                <td>Username</td>
                <td><s:input path="username" /></td>
                <td><s:errors path="username" cssClass="error"></s:errors></td>
            </tr>

            <tr valign="top">
                <td>Password</td>
                <td><s:password path="password"
                        title="One Upper case, one special char, one number, total length 6 to 20 chars. Example: aA1!bcf" /></td>
                <td><s:errors path="password" cssClass="error"></s:errors></td>
            </tr>

            <tr valign="top">
                <td>Age</td>
                <td><s:input path="age" /></td>
                <td><s:errors path="age" cssClass="error"></s:errors></td>
            </tr>

            <tr valign="top">
                <td>Birthday</td>
                <td><s:input path="birthDay" /></td>
                <td><s:errors path="birthDay" cssClass="error"></s:errors></td>
            </tr>

            <tr valign="top">
                <td>Email</td>
                <td><s:input path="email" /></td>
                <td><s:errors path="email" cssClass="error"></s:errors></td>
            </tr>

            <tr valign="top">
                <td>Website</td>
                <td><s:input path="website" /></td>
                <td><s:errors path="website" cssClass="error"></s:errors></td>
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Save" /></td>
            </tr>
        </table>
    </s:form>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Account Info</title>
</head>
<body>

    <h3>Account Info</h3>
    <table cellpadding="2" cellspacing="2" border="1">

        <tr valign="top">
            <td>Username</td>
            <td>${account.username }</td>
        </tr>

        <tr valign="top">
            <td>Password</td>
            <td>${account.password }</td>
        </tr>

        <tr valign="top">
            <td>Age</td>
            <td>${account.age }</td>
        </tr>

        <tr valign="top">
            <td>Birthday</td>
            <td><fmt:formatDate var="birthday" value="${account.birthDay }"
                    pattern="MM/dd/yyyy" /> ${birthday }</td>
        </tr>

        <tr valign="top">
            <td>Email</td>
            <td>${account.email }</td>
        </tr>

        <tr valign="top">
            <td>Website</td>
            <td>${account.website }</td>
        </tr>

    </table>

</body>
</html>
<?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.demo</groupId>
    <artifactId>LearnSpringMVCWithRealApps</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>LearnSpringMVCWithRealApps</name>
    <description>Learn Spring MVC with Real Apps</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- Spring MVC  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- JSTL tag lib -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- Tomcat for JSP rendering -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Screenshots