Sriprem rookie
Sriprem rookie

Reputation: 39

Data not get bind with the Binding result in Spring mvc

I have been facing this problem for the past 2 days. Although I have gone through so many post I did not find a solution for my question.

Actually I was trying to validate my login form using Spring validations but even if I had given some wrong inputs it is not showing me any error , it accepts my every input.

For example, I have used @NotEmpty and @Email annotations for email it even accepts my plain inputs like "user". It has to throw the error as "Invalid email format" but this errors are not get bind into my Bindingresult.

My Controller : ContactController.java

import java.util.ArrayList;
import java.util.List;
import net.viralpatel.spring3.form.loginform;

import javax.validation.Valid;

import net.viralpatel.spring3.form.Contact;
import net.viralpatel.spring3.form.ContactForm;
import net.viralpatel.spring3.form.loginform;

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 org.springframework.web.servlet.ModelAndView;@Controller{
@RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(ModelMap model) {
        loginform ud = new loginform();
         ud.setUser("");
         ud.setEmail("");
        model.addAttribute("lform",ud);
        return "login"; 

    }

    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String loginCheck(@ModelAttribute("lform") @Valid loginform lform, BindingResult result, ModelMap model) {

        if (result.hasErrors()) {
            return "login";
        } else {
            model.addAttribute("lfobj", lform);
            return "success";        

        }
    }

My login.jsp file :

<form:form action="login.html" commandName="lform">
<table>
<tr>
    <td><font face="verdana" size="2px">User</font></td>
    <td>:</td>
    <td>
    <font face="verdana" size="2">
    <form:input path="user" /> <form:errors path="user"></form:errors>
    </font>
    </td>
</tr>
<tr>
    <td><font face="verdana" size="2px">Email</font></td>
    <td>:</td>
    <td>
    <font face="verdana" size="2">
    <form:input path="email" /> <form:errors path="email"></form:errors>
    </font>
    </td>
</tr>
<tr>
    <td><font face="verdana" size="2px">Phone</font></td>
    <td>:</td>
    <td>
    <font face="verdana" size="2">
    <form:input path="phone" /> <form:errors path="phone"></form:errors>
    </font>
    </td>
</tr>
<tr>
    <td><font face="verdana" size="2px">Blog</font></td>
    <td>:</td>
    <td>
    <font face="verdana" size="2">
    <form:input path="blog" /> <form:errors path="blog"></form:errors>
    </font>
    </td>
</tr>
<tr>
    <td>
    <input type="submit" value="Submit" />
    </td>
</tr>
</table>
</form:form>

My loginform.java :

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.BindingResult;

public class loginform{

        @NotEmpty
        private String user;

        @NotEmpty
        @Email
        private String email;        

        @NotEmpty(message = "Phone should not be blank.")
        @Size(min = 10,max = 10)
        private String phone;

        @NotEmpty(message = "Enter your blog URL")
        @URL
        private String blog;
//get & set methods
 }

My spring-servlet.xml :

<context:annotation-config />
    <context:component-scan base-package="net.viralpatel.spring3.controller" /> 

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basename" value="props" />
    </bean>

This is my first program in Spring validation so may be I hit low but kindly give any solutions for my issue.

Upvotes: 0

Views: 3892

Answers (2)

Jaiwo99
Jaiwo99

Reputation: 10017

Add this to your spring config:

<mvc:annotation-driven/>

This will enable Bean Validation.

Document: Reference

Upvotes: 1

Ralph
Ralph

Reputation: 120871

Your jsp form tag is missing the http method attribute method="POST (without this attribute for form data are submit an http GET, and this is bound to the request handler method):

<form:form method="POST" action="login.html" commandName="lform">...

Upvotes: 0

Related Questions