简体   繁体   中英

Spring MVC validations Not working

I am a newbie in spring validations.I have a loginform.html with 4 text fields and i was trying to validate them but the validations are not working in my form. But it accepts each and every value i entered.

For example : If i enter an improper email it should throw me an validation error like "Enter proper Email ID "

loginform.java

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 and set methods}

login.html

<form:form action="login.html" commandName="userDetails">
<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>

ContactController.java

@Controller
public class ContactController {

@RequestMapping(value = "/get", method = RequestMethod.GET)
public String get(ModelMap model) {
    loginform ud = new loginform();
     ud.setUser("prem");
     ud.setEmail("@gmail.com");
    model.addAttribute("userDetails",ud);
    return "login"; 
}

@RequestMapping("/login")
public String loginCheck(@Valid loginform userDetails, BindingResult result, ModelMap model) {

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

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>

success.html

<body>
<font face="verdana" size="2">Welcome Mr. <b>${lfobj.user}</b>,<br>
Validations Success..!<br><br>
<u>You Entered</u><br>
</font>

<table>
    <tr><td>Email</td><td>${lfobj.email}</td></tr>
    <tr><td>Phone</td><td>${lfobj.phone}</td></tr>
    <tr><td>Website</td><td>${lfobj.blog}</td></tr>
</table>
</body>

props.properties

NotEmpty.userDetails.user = User Name is required
NotEmpty.userDetails.email = Email is required

Email.userDetails.email = Enter valid email Id
URL.userDetails.email = Enter valid URL

OUTPUT : While running :(loginform.html - getting inputs)

*User :  sriprem 
Email :  sri 
Phone :  1323sri 
Blog :   nice*

It has show me the eror from my "props.properties" file but it doesnt show just directly gets all my input !!

Success.html

Welcome Mr. sriprem,
Validations Success..!

You Entered
Email   prem 
Phone   1234sri 
Website nice 

In above scenario everything was in wrong format yet it accepts the data ! Can anyone tell me what was wrong in my validations.

Follow the spring documentation:

http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch05s07.html

Have you configure Bean Validation Implementation? Take a look to the point 5.7.3 Configuring a DataBinder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM