I am pretty new in Spring MVC and I have imported a tutorial project related to server side validation and I have some doubt about how exactly it works.
So I have this login page named login.jsp that contain this login form:
<form:form action="${pageContext.request.contextPath}/login" commandName="user" method="post">
<table>
<tr>
<td><label>Enter Username : </label></td>
<td><form:input type="text" path="username" name="username" />
<br> <form:errors path="username" style="color:red;"></form:errors>
</td>
</tr>
<tr>
<td><label>Enter Password : </label></td>
<td><form:input type="password" path="password" name="password" />
<br> <form:errors path="password" style="color:red;"></form:errors>
</td>
</tr>
<tr>
<td> </td>
<td align="center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
that I think use the object specified by the commandName="user" attribute retrieved from the model (correct me if I am wrong) to store the username and password inserted by the user.
This commandName="user" is an instance of this User class:
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotBlank;
public class User {
@NotBlank(message="Username can not be blank")
private String username;
@Size(min=6,message="Password must be atleast 6 characters long")
private String password;
private String gender;
private String vehicle;
private String country;
private String image;
...............................................
...............................................
GETTER AND SETTER METHODS
...............................................
...............................................
}
So as you can see there are the @NotBlank and @Size validation annotation declared on the username and password fields.
And here the first doubt: what exactly is the difference from the 2 used library javax.validation and org.hibernate.validator ?
Why in the tutorial use both? Can I do the same thing using only the hibernate validator library? (I think that I can specify the valid length of a string using the Hibernate validator, or not)?
So then when the login form is submitted it generate and HttpRequest toward the /login resource that is handled by this method declared into a controller class:
@RequestMapping(value="/login" , method=RequestMethod.POST)
public String do_login(HttpServletRequest req , Model md , HttpSession session , @Valid User user, BindingResult br)
{
try
{
//System.out.println(br.getAllErrors().size());
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("Username and pasword are : "+username +" "+ password);
if(br.getAllErrors().size() > 0){
System.out.println("Server side validation takes place....");
}
else{
Login_Model lm = new Login_Model();
String message = lm.do_login_process(username, password);
if(message.equals("login success"))
{
session.setAttribute("username", username);
return "redirect:/myprofile";
}
else
{
md.addAttribute("error_msg", message);
}
}
return "login";
}
catch(Exception e)
{
return "login";
}
}
OK, and now I have the following doubts about this method:
It take this object as input parameter: @Valid User user. Who pass to it? I think that it could depend by the fact that in the form I specified commandName="user" so Spring automatically do it. Is it correct?
From what I have understand the @Valid annotation automatically call the validation process. How it happen? is something related to the AOP feature provided by Spring? or what? Why this @Valid annotation is related only to the javax.validation library and not also to the Hibernate validator (so this @Valid annotation validate also the field annotated with Hibernate validator annotation? why?)
From what I have understand if the user insert wrong values into the login form I can obtain this error by the BindingResult br input parameter. Using the debugger I can see that this object contain the error message defined by the annotation defined into the User model object. How exactly does this work?