Spring MVC Tutorial Hello World

Hi guys ,Today I am going to explain you Spring MVC with a very simple program Hello World in Eclipse IDE .I have explained in tseps the whole procedure If you are facing any problem please write below in comments I ll solve it asap.

Step 1: First you need to Make a Dynamic project in Eclipse. Select file ->Dynamic Web project->Fill Name->Finish.

I gave name as SpringMVCHelloWorld

Step 2: Now you need to add some spring jars to your project lib folder and build path . Please refer to the screenshot below and add the exact version jars otherwise you may face some errors.

Step 3: Now we need to add some more files to the structure .I ll explain the wholw flow at the end ,first let us make the project work .You need

  • JavainhouseController.java
  • Index.jsp
  • WelcomePage.jsp
  • web.xml
  • welcome-servlet.xml

package com.javainhouse;

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

public class JavainhouseController {

public ModelAndView helloWorld() {

   String message =  "Welcome to Javainhouse";

   return new ModelAndView("WelcomePage", "welcomeMessage", message);
}//ModelAndView closed



<title>JavainHouse SpringMVC</title>

<font size="2px" face="verdana">
   <a href="javainhouse.html"><br> Click here</a>







<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">





<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

<context:component-scan base-package="com.javainhouse" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />


Step 4: The directory structure of all the files should be shown as in the screenshot below:

Step 5: Let us Run the project by right click on Index.jsp->Run on server .You ll see both the screens as below :

Execution :

1. web.xml is the very first file read by the spring .In web.xml ,we have given name of two servelts one is welcome and other is Dispatcher Servlet .For Dispacther Servlet we have given load startup as 1 which means it will be the first servlet loaded by spring .

2. After that servlet name is given as welcome ,now spring ll find a filename as welcome-servlet.xml in whole project and read it.

3. In welcome-spring.xml ,we have given <context:component-scan base-package="com.javainhouse" /> which means that in this base packages the Handler mapper need to look for the particular controller for the request given.

4. Index.jsp ll be the first jsp file which ll be loaded at first and display the output to the user .When user click on Click Here link then Handler mappper will start  looking for the controller for this particular request means with Request mapping as javainhouse.

5.  helloWorld() method will be executed and string message will be set.and Model with name welcomeMessage with value "Welcome to Javainhouse" and view WelcomePage ll be returned to the Dispather Servlet .

6. Now in welome-servlet.xml next thing is view resolver which ll be helping to resolve the directory structure of view returened i.e. WelcomePage . If ll prefix  WelcomePage  with jsp and suffix with .jsp i.e. now we ll find WelcomePage.jsp in folder jsp and this view ll be return to the user as shown.

Post a Comment