Scroll to top

Developing a Spring 3 Framework MVC application step by step tutorial

In this article, we will learn how to develop an MVC(CRUD Operation) web application from scratch using the Spring 3 Framework.

Parts of Spring 3 Framework that will be covered in this article.

  • Inversion of Control (IoC)
  • Spring MVC via Annotation
  • Data Access with JDBCTemplate


  • Java SDK 1.6 (Used during this tutorial)
  • Eclipse Indigo (Can also be used Later versions)
  • Spring Framework 3.1
  • HSQLDB v2.2.8 (Any other DB can also be used)

Application to be Developed :

Here in this Spring tutor,ial we are developing an application where we will Insert(Create) User Details in System. Other CRUD operation look here.

Application Setup :

First, create a directory for Spring 3 MVC application. Open Eclipse and go to File >> New >> Other. Then select “Dynamic Web Project” from “Select a Wizard” screen.


Now click “Next“. In next(New Dynamic Web Project) screen provide the name of the application as “JBTSpringMVC” and click “Finish“.


Note*: We have selected “Dynamic Web Module version” as 2.5.

Now the project has been created and the structure of the project would be like.


Add all required Jar in WebContent > WEB-INF > lib . Download all Spring Framework JAR from here .

Required Jars :

  • commons-logging-1.1.1.jar
  • hsqldb.jar (Used for HSQLDB)
  • org.springframework.aop-3.1.1.RELEASE.jar
  • org.springframework.asm-3.1.1.RELEASE.jar
  • org.springframework.beans-3.1.1.RELEASE.jar
  • org.springframework.context-3.1.1.RELEASE.jar
  • org.springframework.core-3.1.1.RELEASE.jar
  • org.springframework.expression-3.1.1.RELEASE.jar
  • org.springframework.jdbc-3.1.1.RELEASE.jar
  • org.springframework.transaction-3.1.1.RELEASE.jar
  • org.springframework.web-3.1.1.RELEASE.jar
  • org.springframework.web.servlet-3.1.1.RELEASE.jar


Configure Web.xml in ‘WEB-INF’ directory:

Now we will modify the web.xml in JBTSpringMVC>>WebContent>>WEB-INF. In web.xml we will define dispatcher servlet(Front End Controller) which will control all request going to web application based on information provided in “url-pattern“. As we want all request url ending with .do to pass through dispatcher, we will provide “<url-pattern>” as “*.do“.

Changed web.xml will look like below.

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



Now we will create a file named “dispatcher-servlet.xml” in JBTSpringMVC>>WebContent>>WEB-INF. This file contains the view resolver details.

Note*: Name of the file (dispatcher-servlet.xml) is not fixed but it depends on the value of <servlet-name> element in web.xml. Whatever servlet-name is defined in web.xml you need to add “-servlet” in it and corresponding .xml will need to be created in given location(WebContent>>WEB-INF).

Define Dispatcher Servlet XML

In dispatcher servlet we will define viewResolve  which help Spring to resolve the exact location of views.

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

		<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    	    <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>



Create Application Context XML

This XML file we will be used for bean definition and AOP related things. This file has already been configured in Spring via “contextConfigLocation”  property in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 <!--  How to include more then one base package -->

 <context:annotation-config />
 <context:component-scan base-package="com.controller,com.beans" />
 <mvc:annotation-driven />

 <!-- Below configuration has been added to enable in memory DB HSQLDB  -->
 <jd:embedded-database id="dataSource1" type="HSQL">
  <jd:script location="classpath:schema.sql"/>
  <jd:script location="classpath:test-data.sql"/>

 <bean id="SpringJdbcDao"  class="com.dao.SpringJdbcDaoImpl">
  <property name="dataSource" ref="dataSource1"/>
  <bean id="SpringJdbcService"  class="com.service.SpringJdbcServiceImpl">
  <property name="springJdbcDao" ref="SpringJdbcDao"/>


Look carefully @ the below code in configuration file

 <context:annotation-config />
 <context:component-scan base-package="com.controller,com.beans" />
 <mvc:annotation-driven />

These lines will enable annotation config and let spring know which package to scan for Controller.


Create Bean Class:

Create a package named “com.beans” and create a bean class ““. This class will have properties of user and getter ,setter for the same.

package com.beans;

public class VngMem {

	private String name;
	private String dob;
	private String email;
	private String phone;
	private String address;
	private Long pincode;
	private String country;

	public VngMem(String name, String dob, String email, String phone,
			String address, Long pincode, String country) {
		super(); = name;
		this.dob = dob; = email; = phone;
		this.address = address;
		this.pincode = pincode; = country;

	public VngMem() {

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getDob() {
		return dob;

	public void setDob(String dob) {
		this.dob = dob;

	public String getEmail() {
		return email;

	public void setEmail(String email) { = email;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) { = phone;

	public String getAddress() {
		return address;

	public void setAddress(String address) {
		this.address = address;

	public Long getPincode() {
		return pincode;

	public void setPincode(Long pincode) {
		this.pincode = pincode;

	public String getCountry() {
		return country;

	public void setCountry(String country) { = country;


Create Controller Class:

Create a package named “com.controller“. Then create controller class “” in given package. Controller class will look like. To let spring know that this is Controller class we have used @Controller annotation.

To map a URL to a method we have used @RequestMapping annotation. Now all the request URL as “” will pass through “insertMemDtls” method. Given URL can be fired via two methods GET & POST. To differentiate between two types of request method property @RequestMapping annotation can be used. As use can in below code.

package com.controller;

 * Code By

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.beans.VngMem;

public class JBTJdbcController {

	com.service.SpringJdbcService Service;

	@RequestMapping(value = "/searchJdbcContact", method = RequestMethod.GET)
	public ModelAndView searchContact() {
		ModelAndView mav = new ModelAndView("JdbcSearch");
		VngMem bean = new VngMem();

		mav.addObject("searchUserGet", bean);
		return mav;

	@RequestMapping(value = "/searchJdbcContact", method = RequestMethod.POST)
	public ModelAndView searchContact(
			@ModelAttribute("searchlist2") VngMem vngmem) {
		ModelAndView mav = new ModelAndView("JdbcSearchResult");

		VngMem bean1 = null;
		try {
			bean1 = Service.searchMemDts(vngmem);
		} catch (Exception e) {

		mav.addObject("searchResultPost", bean1);
		return mav;

	@RequestMapping(value = "/insertJdbcContact", method = RequestMethod.GET)
	public ModelAndView insertMemDtls() {
		ModelAndView mav = new ModelAndView("JdbcInsert");

		VngMem bean = new VngMem();
		mav.addObject("insertUser", bean);
		mav.addObject("status", "success");
		return mav;

	@RequestMapping(value = "/insertJdbcContact", method = RequestMethod.POST)
	public ModelAndView insertContact(
			@ModelAttribute("insertUser") VngMem vngmem) {
		ModelAndView mav = new ModelAndView("JdbcInsert");
		try {
		} catch (Exception e) {
		mav.addObject("searchResultPost", vngmem);
		return mav;

Here in the code, we have used ModelAndView, which will provide the views to be rendered. We are passing a string value to ModelAndView. Once passed Spring will try to resolve the exact view by “viewResolver” bean which we have already defined in dispatcher servlet.

As string name provided is “JdbcInsert” Spring will look for jsp named JdbcInsert.jsp in “/WEB-INF/jsp/” location.


Create the View

To insert user details, we will require a JSP. Which we will create now. Create a JSP named JdbcInsert.jsp in “/WEB-INF/jsp/”. In jsp, we have used form tag from spring framework which will be used to map the property from bean to field in JSP.

<form:form commandName="insertUser" method="POST" action="" id="userdetailsid" >		

				<legend>User details</legend>
						<label for=name>Name</label>
						<form:input path="name"  type="text" placeholder="First and last name" />
						<label for=name>Date</label>
					<form:input path="dob" type="date" required="true" />
						<label for=email>Email</label>
						<form:input path="email" type="text" required="true" />
						<label for=phone>Phone</label>
						<form:input path="phone" type="text" required="true" />
				<legend>User address</legend>

						<label for=address>Address</label>
						<form:input path="address" type="text" required="true" />
						<label for=postcode>Post code</label>
						<form:input path="pincode" type="text" required="true" />
						<label for=country>Country</label>
						<form:input path="country" type="text" required="true" />
				<button type=submit>Save User Details!</button>

Configure Dao Layer

Only thing left now is DAO layer which actually inserts the user details in DB. We are using JdbcTemplate for updating DB. JdbcTemplate will require Datasource, which we have already configured in applicationContext.xml.


package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.beans.VngMem;

public class SpringJdbcDaoImpl implements SpringJdbcDao {

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);

	public void insertMemDts(VngMem contact) {

		String query = "insert into vng_mem (NAME,DOB,EMAIL,PHONE,ADDRESS,PINCODE,COUNTRY)"
				+ " VALUES (?,?,?,?,?,?,?)";

				new Object[] { contact.getName(), contact.getDob(),
						contact.getEmail(), contact.getPhone(),
						contact.getAddress(), contact.getPincode(),
						contact.getCountry() });


	// @Override
	public VngMem searchMemDts(VngMem vngmem) {

		String queryinitial = "select * from vng_mem where NAME ='"
				+ vngmem.getName() + "'";

		System.out.println("query formed with all the argument - "
				+ queryinitial);

		RowMapper rm = null;// = new RowMapper() ;
		List listcontacct = jdbcTemplate.query(queryinitial,
				new RowMapper() {
					public Object mapRow(ResultSet resultSet, int rowNum)
							throws SQLException {
						return new VngMem(resultSet.getString("name"),
								resultSet.getString("dob"), resultSet
										.getString("email"), resultSet
										.getString("phone"), resultSet
										.getString("address"), resultSet
										.getLong("PINCODE"), resultSet

		if (listcontacct.size() > 0)
			return listcontacct.get(0);

		return new VngMem();




Here is the code for

package com.dao;
package com.service;

import com.beans.VngMem;

public interface SpringJdbcService {

	VngMem searchMemDts(VngMem vngmem);

	void insertMemDts(VngMem MemDtlsbean);



package com.service;

import org.springframework.jdbc.BadSqlGrammarException;

import com.beans.VngMem;
import com.dao.SpringJdbcDaoImpl;

public class SpringJdbcServiceImpl implements SpringJdbcService {

	SpringJdbcDaoImpl springJdbcDao;
	public SpringJdbcDaoImpl getSpringJdbcDao() {
		return springJdbcDao;

	public void setSpringJdbcDao(SpringJdbcDaoImpl springJdbcDao) {
		this.springJdbcDao = springJdbcDao;

	public VngMem searchMemDts(VngMem vngmem) {
		try {
			return springJdbcDao.searchMemDts(vngmem);
		} catch (BadSqlGrammarException e) {
		} catch (Exception e) {
		return null;

	public void insertMemDts(VngMem MemDtlsbean) {



Everything is now set up. Now DAO layer can insert user details in DB. But we have used HSQLDB in this project.  If you want to know how to configure HSQLDB please visit here. After configuring HSQLDB start server and click link http://localhost:8080/JBTSpringMVC/

Application Source Code

A Demo application has been deployed on @

Check this application to know how it works. Till now we have covered only ‘C‘ of CRUD operation rest operation will be covered in next Articles.

Note*:: This application till now doesn’t have any kind of validation. Which we will explore in a later article.

Download Project from below link
Author avatar



    can u send the source code?

  2. Ajinkya

    can u send the source code?

  3. Ajinkya

    can u send the source code?

  4. vinodreddy

    please explain with prime frames , jboss server using spring mvc.
    set up set. its very help ful to understand. @[email protected]

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.