The form tag

In this tutorial, you will learn about Spring form tag.


 

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

The form tag

In this tutorial, you will learn about Spring form tag.

This tag returns 'form' tag and provides a binding path to inner tags for binding. The command object is placed in the PageContext, due to this ,inner tags can access command object. Other tags in this library are nested tags of the form tag.

Suppose that we have a object known as Employee and it is a JavaBean having properties like firstName and lastName. In our controller, we can use this object as the form backing object and this returns form.jsp. The form.jsp should look like this :

<form:form>
	<table>
		<tr>
		<td>First Name:</td>
		<td><form:input path="firstName" /></td>
		</tr>
		<tr>
		<td>Last Name:</td>
		<td><form:input path="lastName" /></td>
		</tr>
		<tr>
		<td colspan="2">
		<input type="submit" value="Save Changes" />
		</td>
		</tr>
	</table>
</form:form>

The page controller retrieves firstName and lastName values from the command object placed in the PageContext.

The generated HTML looks like below :

<form method="POST">
	<table>
		<tr>
		<td>First Name:</td>
		<td><input name="firstName" type="text" value="Sabeer"/></td>
		</tr>
		<tr>
		<td>Last Name:</td>
		<td><input name="lastName" type="text" value="Bhatia"/></td>
		</tr>
		<tr>
		<td colspan="2">
		<input type="submit" value="Save Changes" />
		</td>
		</tr>
	</table>
</form>

In the previous example, we are assuming that the variable name of the form backing object is 'command'. You can put the form backing object into the model under another name, after that you can bind the form to the named variable as below :

<form:form commandName="employee">
	<table>
		<tr>
		<td>First Name:</td>
		<td><form:input path="firstName" /></td>
		</tr>
		<tr>
		<td>Last Name:</td>
		<td><form:input path="lastName" /></td>
		</tr>
		<tr>
		<td colspan="2">
		<input type="submit" value="Save Changes" />
		</td>
		</tr>
	</table>
</form:form>

 

Advertisements

Share on Google+Share on Google+

The form tag

Posted on: January 14, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: The form tag  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads