Nearby lessons
21 of 35Spring - MVC Tag Library
- Understand Spring - MVC Tag Library
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring - MVC Tag Library step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
Spring MVC Tag Library
In SPring WEB MVC based applications, to prepare Presentation or View part we are able to use
basic View technologies like HTML, JSP, JSTL , Velocity , Free Marker,......
In Spring WEB MVC based applications, to prepare User Forms Spring WEB MVC framework has
given a seperate tag library.
To use Spring WEB MVC web applications if we want to use Spring provided tag library then we
have to use keep spring-webmvc.jar in web application lib folder and we have to use the following
URL in "uri" attribute in <taglib> directive.
http://www.springframework.org/tags/form
EX: <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Spring has provided the following set of form tags to prepare User forms.
1.<form:form>
It is same as html <form> tag, it will gather/group all other Html components to take data from
users inorder to submit data to Server side application.
Syntax:
<form:form method=-- action=-- commandName=--->
</form:form>
Where method attribute will take HTTP-Method like GET or POST,....
Where action attribute will take action URI of the respective ontroller class.
Where commandName attribute will take Command class object logical name , which
must be same as the value which is specified along with commandName property in
controller class configuration in Spring configuration file inorder to store form data in the
Command class object.
EX:
registrationform.jsp
<form:form method=POST action=reg commandName=user>
</form:form>
User.java
public class User{
----
}
ds-servlet.xml
<beans>
-----
<bean name="reg" class="com.durgasoft.controller.RegistrationController">
<property name="commandName" value="user"/>
<property name="commandClass" value="com.durgasoft.command.User"/>
-----
</bean>
----
</beans>
Spring MVC Tag Library
This tag is same as HTML <input type=text/> tag, it will create textfield in user form , it is an input
GUI component, it will take a line text data from Users.
Syntax:
<form:input path="--" size="--" value="--"/>
Where "path" attribute will take reference name to the text field and it must be same as a
property in Command class having setter and getter method.
Where "size" attribute will take textfield size.
Where "value" attribute will take a default message to display while preparing text field.
<form:form .... >
User Name<form:input path="uname" size="20"/>
</form:form>
In Command class, we must declare "uname" as String property.
public class User{
----
private String uname;
----
public void setUname(String uname){
this.uname = uname;
}
public String getUname(){
return uname;
}
----
}
Spring MVC Tag Library
It will generate password field in user form, it is same as "<input type="password"/>" tag, it will
take password data from users.
Syntax:
<form:password path="--" value="--" showPassword="--"/>
Where "path" atribute will take reference name to the password field, it must be same as
the property defined in the respective Command class having setter and getter methods.
Where "value" atrribute will take default password value to display while creating password
field.
Where "showPassword" property will take either true/false value to show or not to show
password data in password field while providing password data.
EX:
<form:form ----->
User Password<form:password name="upwd" showPassword="false"/>
</form:form>
- Key ideas of Spring - MVC Tag Library explained simply
- Ready-to-use code examples
- Exam-style questions at the end