Signup/Sign In

Adding Data to Spring Model

In this article, we will add data to the Spring Model. The Spring Model is an interface in Spring that is used to add attributes to the model. It provides several methods to handle the data from controller to view.

It mainly used to pass data from the controller to the view page. Let's understand by example.

Methods of Model Interface

The following are the methods of the Model interface.

Modifier and Type Method and Description
Model addAllAttributes(Collection<?> attributeValues) This method is used to copy all attributes in the supplied collection into this Map, using attribute-name generation for each element.
Model addAllAttributes(Map<String,?> attributes) It copies all attributes in the supplied Map into this Map.
Model addAttribute(Object attributeValue) It adds the supplied attribute to this Map using a generated name.
Model addAttribute(String attributeName, Object attributeValue) It adds the supplied attribute under the supplied name.
Map<String,Object> asMap() It returns the current set of model attributes as a Map.
boolean containsAttribute(String attributeName) It returns true if this model contains an attribute of the given name, false otherwise.
Object getAttribute(String attributeName) It returns the attribute value for the given name if any.
Model mergeAttributes(Map<String,?> attributes) It copies all attributes in the supplied Map into this Map, with existing objects of the same name taking precedence.

Project Structure


This is a controller class that contains two methods showForm() and showdata(). The showForm() method is mapped with userForm request and used to return a user-form. Here, we used Spring Model to pass user_name to view(user-data.jsp).

package com.studytonight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class HelloController {

	public String showForm() {
		return "user-form";

	@RequestMapping(value = "showdata", method = RequestMethod.GET)
	public String showdata(HttpServletRequest request, Model model) {
		String name = request.getParameter("user_name");
		return "user-data";

// user-form.jsp

This is our HTML form page that takes user input and submits it to the server at showdata mapping. This request will be handled by the controller(HelloController).

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>User Form</title>
<form action="showdata" method="Get">
	<input type="text" name="user_name"><br/><br/>
	<input type="submit" />

// user-data.jsp

This page shows the input entered by the user in HTML form. The controller returns this page as a response to the client. This page uses the Expression Language to fetch the user form data. Here, we used the name of the attribute that we bind in the controller as model.addAttribute("name", name). We did not use param here, as we did in the previous article.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">

These are Spring configuration XML files that configure views files and dispatcher servlet to provide a front controller for the application.

// spring-mvc-app-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<!-- Step 3: Add support for component scanning -->
	<context:component-scan base-package="com.studytonight.controller" />

	<!-- Step 4: Add support for conversion, formatting and validation support -->

	<!-- Step 5: Define Spring MVC view resolver -->
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />

// web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.1">


	<absolute-ordering />

	<!-- Spring MVC Configs -->

	<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->

	<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns=""
		<!-- -->

Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

This is the second JSP page that shows the user name submitted by the above user name.

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.