Home > Design Patterns, MVC, Spring Framework, Spring MVC 3, Web Development > Spring MVC Tutorial by Example – Controllers

Spring MVC Tutorial by Example – Controllers

Controllers at any common web framework is the part in which a user request is mapped to actual logic behind it, mapped to a method which is certain logic entry point. A controller in a web framework in general and within Spring MVC framework in particular, is a mediator between the view layer and the model layer, which hold the logic of the webapp.

Motivation of using controllers? Imagine you could easily configure URL mapping in which http://…/myhomepage would lead to a certain method within a class and easily respond with needed data back to the user’s browser. This is very nicely achieved with Spring MVC Controllers.

That been said, let’s see a Spring MVC Controller example.

When implementing a controller in spring framework MVC 3 there are only 2 simple configurations to do and implementing a pojo – the controller:

  • two configuration files involved – the spring-servlet.xml and web.xml
  • controller classes whicha are just simple pojos reside in a specific package (configured in spring-servlet.xml) and annotated with spring MVC 3 Controller annotations related.
    (@Controller)
  • Take a look at the project structure print screen below.

Configurations:

spring-servlet.xml(under mywebapp/WEB-INF)
spring-servlet.xml (under mywebapp/WEB-INF)




	
	
	
		
			
		

	
	
	
	
		
		
		
	
	
		
		
			
				classpath:tc-messages
				classpath:ValidationMessages
			
		
		
		
	
	
		
			
			
		
	
	
		
	

 

web.xml (under mywebapp/WEB-INF)- add this in web.xml at the servlets declaration part.
NOTE that the spring dispatcher servlet mapping /app/ will use spring mvc controllers only for url requests which end with http://myapp.com/app/helloworld for example. This is useful and will be discussed later in the sense of other resources(like css, images, js, etc) of the webapp.


  spring
  org.springframework.web.servlet.DispatcherServlet
  1



  spring
  /app/

Spring MVC 3 Controller Example – (its just an annotated ;-) Class(POJO)

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 hello(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView("hello");
		return modelAndView;
	}
}

The important part in the code above is the @RequestMapping(value = “/hello”) above the hello method. In the background a URL mapping is created at runtime and when a user will send request with http:../app/hello he/she will end up in this method. This entry point also automatically gives access to the HttpRequest, HttpResponse and can also declare URL parameters mapping in the method signature (Later post).

Your project should look like this:
Spring MVC Controller Example

 

Spring MVC Controllers Example – Conclusion

Implementing a controller in Spring MVC 3 is very simple. It encapsulates easy access to expected web components like the request, response, request parameters session and a real easy URL mapping between a user’s request and a serving entry point method. It also encapsulates easy dybnamic URL mapping which is very useful especially when implementing REST services (on a later post).

I highly recommend adding Spring MVC Controllers into your toolbox – I did!

Relevant SpringFramework documentation: Spring MVC Controllers – Documentation

 

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