Programsji's Spring Tutorials

Hello World(Spring MVC)

Spring Hello World

Welcome to the world of Spring MVC. We are going to learn some very basic Spring MVC terms. You can see the final output of this program on the image left. We will learn these terms.
(1)What are configuration Files?
(2)Why we use @Controller & @RequestMapping Annotations?
(3)What are Models?
(4)What are ViewResolver?
(5)How we send data from Controller to view(jsp)
(6)What is new in web.xml file and many other things?

Common Tags(Spring MVC)

Spring Common Tags Example

In this tutorial, we will learn about some commons tags of spring MVC. Some of common tags are given below:-
(1)<s:form />
(2)<s:input />
(3)<s:checkbox />
(4)<s:checkboxes />
(5)<s:radiobutton />
(6)<s:hidden />
(7)<s:select />
we will learn all these tags working In detail, as how to bind model’s properties in these or how to retrieve those values from server side etc. You will also learn working of @ModelAttribute annotations. Lets start...

Pathvariable(Spring MVC)

Spring Path Variable Example

Till now, we have learnt that when we type http://http://localhost:8080/myurl,then control will move to the controller’s method which is mapped with this url. Now have a look at this url: http://http://localhost:8080/myurl/ProgramSji.Here Programsji is a pathvariable. You can get this value in any variable. And use that value in your code. Let’s start with this program…..

Requestparam(Spring MVC)

Spring Requestparam Example

in this url, fname=Steve&lname=Gates is querystring, we can access these querystring's values in our method using RequestParam . Let’s Learn this program step by step….

MultiAction(Spring MVC)

Spring Multiaction Example

Here we will learn ,how to map urls to methods.In this program, we are going to create multiple controllers , then multiple methods in each controllers, and multiple urls to map to those methods. Lets see…

CRUD(Spring MVC)

Spring CRUD Example

(Create Retrieved, Update Delete), this is the most common program, that we use in many applications. Here we will learn many new things, Like
(1) Database connection
(2) Hibernate with Spring
(3) HQL
(4) Multiple Actions and many more.
Let’s start to learn this program line by line.

Ajax(Spring MVC)

Spring Ajax Example

In this program, we have two dropdowns. In first dropdown, we have a list of countries,when we change first dropdown’s value, then it will send an ajax request and retrieve states from server side according to country selection. Here you will learn some new things, like, because we are sending here country name In json format (in ajax command),and receiving response in json format, so for json support we have to add “org.codehaus.jackson” dependency in pom.xml file, so please check pom.xml file also. And have to create a bean of type MappingJacksonHttpMessageConverter.So that whatever json data is coming to action method, that can be converted into requestparam. And whatever data we are sending from server to client, it can be converted into json. so please check configuration file also We will also learn @RequestParam, and @ResponseBody annotations. Let’s start the program….

Converter(Spring MVC)

Spring Converter Example

Converters are used to Convert http messages into some usable form, for example,to convert json message into a bean (or bean’s properties into json), we use MappingJacksonHttpMessageConverter. we can also create our own converters as per our specific need.
In this program, we are going to create a class Person, then we’ll send “Bill;Jobs;55;1000” from client side to server, Using our converter, these values will automatically create an object of Person class and put these values in person’s fname, lname,age and salary properties. and send that to action method. Lets start….
Here, we will learn some new things, like to create our own conveter, we have to implement Converter interface. and then we have to register this converter in configuration file. So please have a look at that file too.

Interceptor(Spring MVC)

Spring Interceptor Example

In this example, we are going to implement HandlerInterceptor, So whenever Program will get some request from client side and if that request url is configured with Interceptor, then these methods will be executed in a sequence
(1)Interceptor’s preHandle
(2)Action method, that is mapped with url
(3)Interceptor’s postHandle
(4)Then it will render view (jsp)
(5) At the end, Interceptor’s afterCompletion will be executed.
Any of the above interceptor we will use here in this program, that must be configured into configuration file. So please have a look also in configuration file.

Exception Handling(Spring MVC)

Spring Exception Handling Example

We already know, what are exceptions, and how we handle it, Spring offers us a facility to create a common method to handle exceptions for all methods of same controller. Lets have a look at this program…

Global Exception Handling(Spring MVC)

Spring Global Exception Handling Example

Spring offer us a class SimpleMappingExceptionResolver, that can be inherited to create own Exception handler.
Whenever we create our own Exception Hander, we have to put entry in configuration file. So please have a look at configuration file also. Lets have a look at this program.

Validation Using Annotation(Spring MVC)

Spring Validation Using Annotation Example

Suppose we fill a form online, to validate all values on the form ,we use some kind of validation on server side. Here in spring, we can use validation annotations on bean’s property. For example, if we need person bean’s name field’s length must be between 5 to 20, we can use this annotation on name property @Length(min=5 ,max=20,message=”please check length”) Spring will only read these validations annotations, when we use @valid annotation with @ModelAttribute on post method. Whenever we have to add this kind of validation , we need to add dependency for these annotations. So please have a look at pom.xml for those dependencies. Let’s have a look at this program to see @valid annotation and other validation annotations working.

Validation Using Validator(Spring MVC)

Spring Validation Using Validator Example

To validate data that server is receiving for client side, we can use Spring’s validator interface, You can create different validators for each model by implementing validator interface. Let’s see validator interface’s working.

Spring With Tiles(Spring MVC)

Spring with Tiles

In our application, if we have multiple pages those share a common header/footer,then it is a good idea to use tiles. Tiles offers us functionality to use same header/footer for multiple pages.
When we need to use tiles, in our application, (1)we have to include its dependencies into pom.xml (2) have to create beans for class TilesConfigurer and TilesViewResolver. (3)one new file will be created tiles.xml.
Lets see how spring helps us to integrate tiles with it.

Upload a file(Spring MVC)

Upload a file

It is very common in application to add functionality to upload a file. Here we will learn how we can upload a file using Spring MVC framework and how to save that file on server.
If we need to add functionality to upload a file, we need to register a Class CommonsMultipartResolverin configuration file.
Let’s start with this program…

Download a file(Spring MVC)

Download a file

It is very common in application to add functionality to download a file. Here we will learn how can we download a file using spring MVC.

Locale(Spring MVC)


If your application is multilingual, you may need to add functionality to change all messages or labels according to locale. For that you have to create different properties files, for each locale, where you can put your messages. For example, if you want to add functionality of three locales(English, Chinese, Italy), we have to add three files.,, Here Messages is just a name, that can be changed. Lets have a look at the program, then only we can understand

Send a Mail(Spring MVC)

Spring Send a Mail Example

In this program, we will learn, how to send a mail from spring MVC application. We are going to send a mail using gmail domain. Lets have a look at this program.