Followers

springMVC : Spring Form validation Using Validator(Annotation)

Spring Form validation Using Validator(Annotation)

index.jsp
======

New Users Registration : <a href="register.htm" >Here</a>

web.xml
======

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


dispatcher-servlet.xml
===============

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

   
    <context:component-scan base-package="myspring" />
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
 
</beans>

MyController.java
===========

package myspring;

import javax.validation.Valid;
//import javax.validation.Validator;// don't use this import.
import org.springframework.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/register.htm")
public class MyController {
    @Autowired
    private Validator validator;
    public Validator getValidator() {
        return validator;
    }
    public void setValidator(Validator validator) {
        System.out.println("===setValidator===");
        this.validator = validator;
    }
    @InitBinder
    public void initBinder(WebDataBinder binder){
        System.out.println("In initBinder.");
        binder.setValidator(validator);
        //Comment above line and execute, Validations will not work.
    }
   
   
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap map){
        User user = new User();
        map.addAttribute("user", user);
   
    return "/WEB-INF/jsp/register.jsp";
    }
   
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@Valid User user,BindingResult result){
        if(result.hasErrors()){
    return "/WEB-INF/jsp/register.jsp";
        }
    return "/WEB-INF/jsp/success.jsp";
    }
}

User.java
======

package myspring;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

public class User {
    @NotEmpty(message = "User Must not be Blank.")
    private String name;
    @NotEmpty(message = "Pass Word Must not be Blank.")
    @Size(min = 8, max = 10, message = "Pass Word should be between 8 and 10")
    private String passWord;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
  
}


register.jsp
=======

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form method="POST" commandName="user" >
   Enter the user name : <form:input path="name" /><form:errors path="name" /> <br />
   Enter the pass word : <form:password path="passWord" /><form:errors path="passWord" /> <br />
   <input type="submit" value="Login" />
   
</form:form>

success.jsp
=======

Hi,
Login of ${user.name} is successful.


No comments:

Post a Comment