Spring MVC Tag Library

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, etc.

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.

E.g:

<%@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

“action” attribute will take action URI of the respective controller class.

“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 in order to store form data in the Command class object.

E.g

registrationForm.jsp

<form:form method="POST" action="reg" commandName="user">
</form:form>
2. <form:input>

This tag is same as HTML <input type=‖text‖/> tag, it will create text field 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.

“size” attribute will take text field size.

“value” attribute will take a default message to display while preparing text field.

E.g

<form:form .... >
   User Name<form:input path="uname" size="20"/>
</form:form>
3. <form:password>

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.

“value” atrribute will take default password value to display while creating password field.

“showPassword” property will take either true/false value to show or not to show password data in password field while providing password data.

E.g

<form:form ----->
    User Password<form:password name="upwd" showPassword="false"/>
</form:form>
4. <form:checkbox>

It will provide checkbox in user form, it is same as Html <input type = “checkbox”> tag and it will submit a boolean value to the Server side application on the basis of the selection. If we select checkbox then it will send “true” value , if we are not selecting checkbox then it will send false value to server side application.

Syntax:

<form:checkbox path="--" value="--"/>

Where

“path” attribute will take reference name to the checkbox, it must be same as the boolean property existed in Command class having setXXX() and isXXX() method.

“value” will take a value to send to the Server side application, where at Server side Command class we need a property of String or xxx data type with setXXX() and getXXX() method.

<form:form --- >
   Are you Married? <form:checkbox path="maritalStatus" value="Married"/>
</form:form>
5. <form:checkboxes>

It will provide collection of checkboxes to select items.

Syntax:

<form:checkboxes path="--" items="--"/>

Where

“path” attribute will take reference value of all checkboxes , it must have a property of String[] in the respective command class having setXXX() and getXXX() methods.

“items” attribute will take an expression including a property from Command class providing checkbox labels and values or a Property data which is generated by overriding referenceData() method in controller class.

E.g

<form:form ---- >
     User Quaqlifications<form:checkboxes path="uqual" items="{qual_List}"/>
</form:form>
6. <form:radiobutton>

It will display a radio button to select an item, it is same as html.

Syntax

<form:radiobutton path="--" value="--"/>

Where

“path” attribute will take a reference name , it must be same as the property in Command class having setXXX() method and getXXX() method.

“value” attribute will take radio button value inorder to send to Server side application.

E.g

<form:radiobutton path="ugender" value="Male"/>Male
<form:radiobutton path="ugender" value="Female"/>Female
7. <form:radiobuttons>

It will provide multiple radio buttons as a group to select one item.

Syntax

<form:radiobuttons path="--" items="--"/>

Where

“path” attribute will take reference name to the radio buttons which same as the property name in command class inorder to store the seleted radio button value.

“items” attribute will take the property name of the List or String[] which contains all the names of the Radio Buttons which is generated from referencedData() method from the respective controller class.

E.g

<form:radiobuttons path="uworkLocation" items="${uworkLocation}"/>
8. <form:select>

<form:select> tag can be used to create select box, it is same as html <select> tag, it will display list of items to select either multiple or single depending on our option.

Syntax

<form:select path="--" multiple="--" items="--">
   ------
</form:select>

Where

“path” attribute will take reference value to the Select box , it must be same as the respecitve property name in command class inorder to store the selected values.

“multiple” attribute will take either “true” or “false” value inorder make “multiple” selections or “single” selection in the select box.

“items” attribute will take a property name of Map type contains the Item values and itemLabels which is generated from referenceddata() method of the contoller class.

Note

If we want to provide items to the select box individually , not as a Map from controller class then we have to use <form:option> tag as chaild tag to <form:select> tag.

Syntax

<form:option value="--"> Label </form:option>

Where

“value” attribute will take item Value which we want to send to Server side application.

“Label” is item Label to display in select box.

Note

If we want to get all the Item Values and Item Labels from referencesData() method in Controller class then we have to use <form:options> tag.

Syntax

<form:options items="---" itemValue="--" itemLabel="---"/>

Where

“items” attribute will take a property representing Map object contains item values and item Labels from referencedData() method in Controiller class.

E.g

<form:select path="uskillSet" multiple="true">
   <form:option value="C">C</form:option>
   <form:option value="C++">C++</form:option>
   <form:option value="Java">Java</form:option>
   <form:option value=".Net">.Net</form:option>
   <form:option value="Oracle">Oracle</form:option>
</form:select>
9. <form:textarea>

It will provide text area to take multiple lines of data from Users, it is same as <textarea> tag in Html.

Syntax

<form:textarea path="--"/>

Where

“path” attribute will take reference name to the textarea which is same as a property in Command class.

10. <form:hidden>

It will prepare Hidden field in user forms, it is same as <input type = “hidden”/> in Html.

Syntax

<form:hidden path="--" value="--"/>

Where

“path” attribute will take a reference name to the hidden field, it must be same as a property in Command class having setXXX() method and getXXX() method.

E.g

<form:hidden path="sid" value="S-111"/>
Spring MVC Tag Library

Scroll to top