Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Triggering @Controller Input Validation in Spring

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 303
    Comment on it

    Triggering @Controller Input Validation:- In Spring we can enable the JSR 303 Bean Validation to put the validation on bean and validate the form value in spring MVC .

    StudentController.java

    package com.manish.controller;
    
    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.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.manish.model.Student;
    
    @Controller
    @RequestMapping("/student")
    public class StudentController {
    
        @RequestMapping(value = "/register", method = RequestMethod.POST)
        public String addStudent(
                @Valid Student student,
                BindingResult result) {
    
    
            if (result.hasErrors()) {
                return "Registration";
            } else {
                return "Success";
            }
    
        }
    
        @RequestMapping(method = RequestMethod.GET)
        public String displayStudentForm(ModelMap model) {
    
            model.addAttribute("student", new Student());
            return "Registration";
    
        }
    
    }
    

    Student.java

    package com.manish.model;
    
    import org.hibernate.validator.constraints.NotEmpty;
    import org.hibernate.validator.constraints.Range;
    
    public class Student {
    
        @NotEmpty
        String name;
    
        @Range(min = 18, max = 21)
        int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Student() {
        }
    
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
    
    }
    

    app-context.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <context:component-scan base-package="com.manish.controller" />
    
        <mvc:annotation-driven />
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    
        <bean class="org.springframework.context.support.ResourceBundleMessageSource"
            id="messageSource">
            <property name="basename" value="messages" />
        </bean>
    
    </beans>
    

    Registration.jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <html>
    <head>
    <style>
    .error {
        color: #ff0000;
    }
    
    .errorblock {
        color: #000;
        background-color: #ffEEEE;
        border: 3px solid #ff0000;
        padding: 8px;
        margin: 16px;
    }
    </style>
    </head>
    
    <body>
        <h2>Student Registration</h2>
    
        <form:form method="POST" commandName="student" action="student/register">
            <form:errors path="*" cssClass="errorblock" element="div" />
            <table>
                <tr>
                    <td>Name :</td>
                    <td><form:input path="name" /></td>
                    <td><form:errors path="name" cssClass="error" /></td>
                </tr>
                <tr>
                    <td>Age :</td>
                    <td><form:input path="age" /></td>
                    <td><form:errors path="age" cssClass="error" /></td>
                </tr>
                <tr>
                    <td colspan="3"><input type="submit" /></td>
                </tr>
            </table>
        </form:form>
    
    </body>
    </html>
    

    Success.jsp

    <html>
    <body>
        <h2>Successfullly Registered</h2>
    </body>
    </html>
    

    message.properties

    NotEmpty.student.name = Name is required!
    Range.student.age = Age between 18 and 21 only
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: