HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

java-spring: Controllers in spring mvc?

Could you plz explain the contollrs concept with examples. How many types of controllers existed in spring mvc. provide the names of the controllers.

Explain each controller concept with suitable examples.

java x 210
spring x 24
Posted On : 2016-08-22 07:31:27.0


The notion of a controller is part of the MVC design pattern (more specifically it is the ´C´ in MVC. Controllers provide access to the application behavior which is typically defined by a service interface. Controllers interpret user input and transform such input into a sensible model which will be represented to the user by the view. Spring has implemented the notion of a controller in a very abstract way enabling a wide variety of different kinds of controllers to be created. Spring contains form-specific controllers, command-based controllers, and controllers that execute wizard-style logic, to name but a few.

Spring´s basis for the controller architecture is the org.springframework.web.servlet.mvc.Controller interface, the source code for which is listed below.

public interface Controller {

* Process the request and return a ModelAndView object which the DispatcherServlet
* will render.
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;


As you can see, the Controller interface defines a single method that is responsible for handling a request and returning an appropriate model and view. These three concepts are the basis for the Spring MVC implementation - ModelAndView and Controller. While the Controller interface is quite abstract, Spring offers a lot of Controller implementations out of the box that already contain a lot of the functionality you might need. The Controller interface just defines the most basic responsibility required of every controller; namely handling a request and returning a model and a view.

For more information kindly visit: Spring-IO Docs Spring Controllers

Spring Web MVC Framework Example:
* Create the request page (optional)
* Create the controller class
* Provide the entry of controller in the web.xml file
* Define the bean in the xml file
* Display the message in the JSP page
* Load the spring core and mvc jar files
* Start server and deploy the project
* Directory Structure

To run this example, you need to load:

* Spring Core jar files
* Spring Web jar files

If you are using Maven, you don´t need to add jar files. Now, you need to add maven dependency in pom.xml file.

<project xmlns="" xmlns:xsi=""
<name>SpringMVC Maven Webapp</name>



1) Create the request page (optional):
This is the simple jsp page containing a link. It is optional page. You may direct invoke the action class instead.

<a href="hello.html">click</a>

2) Create the controller class:
To create the controller class, we are using two annotations @Controller and @RequestMapping.

The @Controller annotation marks this class as Controller.

The @Requestmapping annotation is used to map the class with the specified name.

This class returns the instance of ModelAndView controller with the mapped name, message name and message value. The message value will be displayed in the jsp page.
package in.anyforum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
public class HelloWorldController {
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);

3) Provide the entry of controller in the web.xml file:
In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"

4) Define the bean in the xml file:
This is the important configuration file where we need to specify the ViewResolver and View components.

The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.

Here, the InternalResourceViewResolver class is used for the ViewResolver.

The prefix+string returned by controller+suffix page will be invoked for the view component.

This xml file should be located inside the WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<context:component-scan base-package="in.anyforum" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />

5) Display the message in the JSP page:
This is the simple JSP page, displaying the message returned by the Controller.

It must be located inside the WEB-INF/jsp directory for this example only.

Message is: ${message}

Posted On : 2016-08-22 22:46:15
Satisfied : 1 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -