You are on page 1of 4

Writing a Simple Controller using Spring MVC

Description: Display the view on the browser that is configured. I have made an
attempt to write a basic controller of spring.

Things to be done to attain the above objective are as follows

Step 1: Configuration of DispatcherServlet of Spring in the web.xml

All the requests in spring are handled by RequestDispatcher, if the name of the servlet
is demo then we need to have a spring configuration file with the name
demo-servlet.xml in the WEB-INF directory
Step 2: Assume that if all our requests have to pass through spring controller before
the view is rendered. The Controller should be as below

The controller will return ModelAndView Object, where the name of the view is
index.
Step 3: We need to configure this controller in the spring configuration file
i.e. demo-servlet.xml, the spring container loads the controller through IOC

In Spring MVC we are required to configure the following

1. Controller – Similar to servlet in J2EE


2. Handler Mapping – It is the mapping of the URL to the specific Controller
3. View Resolver – the suffix for the view that is returned by the Controller i.e.
index in Step 2 which will be index.jsp. viewClass is the madatory attribute.

Step 4: Prepare the view index.jsp


Step 5: Execution of the above if every thing is configured in your web server
properly should be as follows

Reference:

http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html

Next Blog: Using Spring Form Controller

You might also like