Reputation: 989
I have a doubt that in our web applications generally we are using spring MVC and for view purpose Spring MVC tags, So while writing Spring MVC tags we are using <form:xxx>
tags like <form:input>
<form:option>
<form:select>
also <form:label>
like this. My confusion is that generally in HTML we are using all these above tags without including <form:xxx>
so what is the exact difference in between both the tags and why <form:xxx>
tag is needed when it comes to Spring MVC. Is there any object associates to it or what?
Upvotes: 2
Views: 414
Reputation: 971
If you want to bind the user values collected using form to Object, Then you will need to use Spring form tag. For example..
<form:form action="actionUrl" method="post" modelAttribute="loginForm">
<form:input path="empId" placeholder="Enter Employee Id"/>
<form:errors path="empId" cssClass="error"/>
<form:password path="password" placeholder="********"/>
<form:errors path="password" cssClass="error"/>
<input type="submit" value="Login"/>
</form:form>
Here modelAttribute="loginForm"
, loginForm is a Object of Java Class with two fields empId
and password
.
public class LoginForm{
private Integer empId;
private String password;
//getters and setters
}
So, Now when you submit the Login form, the values are automatically bound to java object which you can access in your controller method. Like
@RequestMapping(value = {"/login"},method = RequestMethod.POST)
public String authLogin(@Valid LoginForm loginForm,BindingResult result,ModelMap model){
//Your code logic
}
So, spring form binds form values automatically to java object and saves from accessing each request parameter manually. These values can also be validated using validation framework.
Upvotes: 1