Home > J2EE, MVC, Spring Framework, Spring MVC 3 > Spring MVC3 Tutorial – Inroduction | Setup | Hello World

Spring MVC3 Tutorial – Inroduction | Setup | Hello World

February 21st, 2012 Leave a comment Go to comments

Spring MVC3 is a framework for building web applications:

In this post I will detail in the most simple way how to start with Spring MVC3. I will use Eclipse and JBoss AS 7.

Note that there are Maven archtypes which can be used for creatin Spring MVC projects. I will not use it in this post.

A spring MVC3 project is not different than any other web project. What makes is a Spring MVC3 project is the fact that requests are dispatched to a  Spring DispatcherServlet instance which handles the request in terrms of:

1. Controllers - User Friendly URLs and Request Params, Session Handling

2. Handling Forms - Form Input to Objects Binding

3. Internationalization

4. Exception Handling

5. Ajax – Ajax Handling Via Controllers

6. Injecting Resources - for Example Injecting EJB3

7. Form Validation - Server Side Forms Input Validation

8. REST Services – Via Controllers

It works Great! Let’s start!

1. Create a web application in eclipse (Indigo):

Seletc file–>New–>Dynamic Web Project

2.  Adding Dependencies

2.1 Download Dependencies by downloading the Spring Framework jars: Spring Framework Download

2.2 Extract the downloaded zip file and place it under: MyProject/WebContent/WEB-INF/lib

2.3 Download 3 more dependencies:

  1. Apache Commons Logging
  2. jstl1.1 and jstl-impl

Your project after downloading all dependencies should look like this:

Spring MVC3 lib directory

Now just set the build path to include the above dependencies jars you just placed under the WEB-INF/lib

3. Creating a Controller

The hurt of a Spring MVC 3 project are the controllers which are simple Java classes annotated with @Controller just above the class declaeration.

Your web project has a Java Resources directory under this directory there’s the src directory.

  1. Right click on src–>Create new Class–>Under package set my.com.controllers and for class name set: MyController

spring mvc 3 controller

MyController.java

package my.comp.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

	@RequestMapping(value = "/hello")
	public ModelAndView pulse(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView("hello");

		return modelAndView;
	}

}

 

3.2 Create a jsp File

Under MyProject/WebContent/jsp create a jsp file and name it: hello.jsp
hello.jsp







	Hello Spring MVC3!


4. Configuring web.xml and spring-servlet.xml to make our project a Spring MVC3 project

In the step above we created a controller and a jsp file. Now we want to make a user request of the following form:

http://localhost:8080/myapp/hello to be mapped on our controller, MyController, will map between hello.jsp file and a /hello request. This will be achieved by @RequestMapping(value = “/hello”) annotation at the controller above a certain method. The method return a ModelAndView instance which set hello as itsd view. Behibe the scenes content of hello.jsp will be returned as a response.

Now let’s see wow does this happen? It’s easy there is just two more parts:

  1. web.xml – declaring a listener for requests and dispatch them to a spring DispatcherServlet from here everything is behind the scenes
  2. spring-servlet.xml this is where we configure our spring mvc3 application.

Add the following to your web.xml file:

 

    spring
    org.springframework.web.servlet.DispatcherServlet
    1
  
  
    spring
    /app/*
  

spring-servlet.xml file:




	
	
	
		
			
		

	

	

	
        
	
		
		
		
	

	
		
		
	

	
	

Now let’s test!
In order to run the project on the server the easiest way is to right click on the project and select run on server.
OR you can export the project as a war file and deploy (Exporting as war is just a matter of right click on the project and export as war)

After deploying the application your app should be available at:
http://localhost:8080/spring_mvc3_app/app/hello

Note that spring servlet dispatcher, dispatches every request from the URL template myapp/app/* as configured in the web.xml.

 

 
Summary
Spring MVC3 is simple and and works good for main components of a webapplication. The hurt of Spring MVC is in it’s Controllers which are just simple classes with methods for implements logic while annotations are mapping between a URL pattern to certain method behaviour. The annotations as well as the configurations are failrly simple and intuitive. From my experience JBoss, Tomcat, and Weblogic are support MVC3 with no special tweaks.

  1. November 19th, 2012 at 22:13 | #1

    Good explanation, Nice job.

  1. No trackbacks yet.
You must be logged in to post a comment.