DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

Spring Form Tags Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (5)
Save
Tweet
Share
361.86K Views

Join the DZone community and get the full member experience.

Join For Free

in this example you will see how to populate the form with dynamic values. here is the user registration form.

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>registration page</title>
</head>
<body>

<form:form method="post" commandname="user">
<table>
    <tr>
        <td>user name :</td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td>password :</td>
        <td><form:password path="password" /></td>
    </tr>
    <tr>
        <td>gender :</td>
        <td>
            <form:radiobutton path="gender" value="m" label="m" /> 
            <form:radiobutton path="gender" value="f" label="f" />
        </td>
    </tr>
    <tr>
        <td>country :</td>
        <td>
            <form:select path="country">
            <form:option value="0" label="select" />
            <form:options items="${countrylist}" itemvalue="countryid" itemlabel="countryname" />
            </form:select>
        </td>
    </tr>
    <tr>
        <td>about you :</td>
        <td><form:textarea path="aboutyou" /></td>
    </tr>
    <tr>
        <td>community :</td>
        <td><form:checkboxes path="communitylist" items="${communitylist}" itemvalue="key" itemlabel="value" /></td>
    </tr>
    <tr>
        <td></td>
        <td>
        <form:checkbox path="mailinglist" label="would you like to join our mailinglist?" />
        </td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="register"></td>
    </tr>
</table>
</form:form>

</body>
</html>

here we populate the countrylist and the communitylist from the back-end. the items attribute holds the collection. the itemvalue and itemlabel attributes holds the key and value respectively. itemlabel is the one that will be displayed to the user and itemvalue is the one that will be passed when that particular item is selected.

here we have three domain objects user, country and community. the user object is the one that is associated with the form.

package com.vaannila.domain;

import java.util.list;

@suppresswarnings("unchecked")
public class user {

	private string name;
	private string password;
	private string gender;
	private string country;
	private list countrylist;
	private string aboutyou;
	private string[] community;
	private list communitylist;
	private boolean mailinglist;
	
	public string getname() {
		return name;
	}
	public void setname(string name) {
		this.name = name;
	}
	public string getpassword() {
		return password;
	}
	public void setpassword(string password) {
		this.password = password;
	}
	public string getgender() {
		return gender;
	}
	public void setgender(string gender) {
		this.gender = gender;
	}
	public string getcountry() {
		return country;
	}
	public void setcountry(string country) {
		this.country = country;
	}
	public list getcountrylist() {
		return countrylist;
	}
	public void setcountrylist(list countrylist) {
		this.countrylist = countrylist;
	}
	public string getaboutyou() {
		return aboutyou;
	}
	public void setaboutyou(string aboutyou) {
		this.aboutyou = aboutyou;
	}
	public string[] getcommunity() {
		return community;
	}
	public void setcommunity(string[] community) {
		this.community = community;
	}
	public list getcommunitylist() {
		return communitylist;
	}
	public void setcommunitylist(list communitylist) {
		this.communitylist = communitylist;
	}
	public boolean getmailinglist() {
		return mailinglist;
	}
	public void setmailinglist(boolean mailinglist) {
		this.mailinglist = mailinglist;
	}
	
		
}

the user object has a countrylist and communitylist to hold the list of countries and communities respectively.

the countrylist contains a list of country objects.

ackage com.vaannila.domain;

public class country {

	private int countryid;
	private string countryname;
	
	public country(int countryid, string countryname)
	{
		this.countryid=countryid;
		this.countryname=countryname;
	}
	
	public int getcountryid() {
		return countryid;
	}
	public void setcountryid(int countryid) {
		this.countryid = countryid;
	}
	public string getcountryname() {
		return countryname;
	}
	public void setcountryname(string countryname) {
		this.countryname = countryname;
	}
	
}

the countryid is used to refer the country in the back-end and the countryname to dispaly the country in the front-end.

similarly the communitylist contains a list of community objects.

package com.vaannila.domain;

public class community {

	private string key;
	private string value;
	
	public community(string key, string value)
	{
		this.key = key;
		this.value = value;
	}
	
	public string getkey() {
		return key;
	}
	public void setkey(string key) {
		this.key = key;
	}
	public string getvalue() {
		return value;
	}
	public void setvalue(string value) {
		this.value = value;
	}
	
}

here the value is the one that will be dispalyed in the front-end and the key is the one that will be used in the back-end.

in the controller class you need to override the referencedata() method. in this method you can set all the default values that should be loaded when the form is displayed to the user. this method will be called automatically.

package com.vaannila.web;

import java.util.hashmap;
import java.util.map;

import javax.servlet.http.httpservletrequest;

import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.simpleformcontroller;

import com.vaannila.domain.user;
import com.vaannila.service.userservice;

@suppresswarnings("deprecation")
public class usercontroller extends simpleformcontroller {

	private userservice userservice;

	public usercontroller()	{
		setcommandclass(user.class);
		setcommandname("user");
	}
	
	public void setuserservice(userservice userservice) {
		this.userservice = userservice;
	}
	
	@suppresswarnings("unchecked")
	@override
	protected map referencedata(httpservletrequest request) throws exception {
		map referencedata = new hashmap();
		referencedata.put("countrylist", userservice.getallcountries());
		referencedata.put("communitylist", userservice.getallcommunities());
		return referencedata;
	}

	@override
	protected modelandview onsubmit(object command) throws exception {
		user user = (user) command;
		userservice.add(user);
		return new modelandview("usersuccess","user",user);
	}

}

in the referencedata() method we first create a hashmap and add the countrylist and the communitylist to it. this method will be called before the form is rendered so the list will be populated before that.

when you run the example you will see the user registration form. on submitting the form the usersuccess.jsp page will be displayed. in the usersuccess.jsp page we use jstl tags to display the details.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>success page</title>
</head>
<body>
user details
<hr>
user name   : <c:out value="${user.name}"></c:out> <br/>
gender      : <c:out value="${user.gender}"></c:out> <br/>
country     : <c:out value="${user.country}"></c:out> <br/>
about you   : <c:out value="${user.aboutyou}"></c:out> <br/>
community   : 
<c:foreach var="community" items="${user.communitylist}" >
	<c:out value="${community}"></c:out>
</c:foreach> <br />
mailing list: <c:out value="${user.mailinglist} "></c:out>
</body>
</html>

you can download and try the example here.

source : download
war : download
Form (document)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 11 Git Commands That Every Developer Should Know
  • 10 Easy Steps To Start Using Git and GitHub
  • Little's Law and Lots of Kubernetes
  • Test Design Guidelines for Your CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: