SpringMVC HelloWorld Tutorial

Spring is a popular Application framework based on Inversion Of Control/DependencyInjection principle.
SpringMVC is a framework following MVC architecture for building web applications.

Let us see how to create simple Hello World application using SpringMVC.

Step1 :
Copy the SpringMVC dependent libraries into WEB-INF/lib folder.
You can download the latest Spring bundle at http://www.springsource.org/download

Step2:
Configure the Spring’s FrontController class DispatcherServlet in web.xml.
You can configure the other spring beans (if any) in XMLs and wireup using ContextLoaderListener as follows:

 

dispatcher
org.springframework.web.servlet.DispatcherServlet
1


dispatcher
*.htm



org.springframework.web.context.ContextLoaderListener


contextConfigLocationclasspath:applicationContext.xml


Step3:
Create dispatcher-servlet.xml and put it in WEB-INF folder.
















Step4:
Create index.jsp as follows:

<html>
<head>
<title>SpringMVC - HelloWorld</title>
</head>

<body>
Enter your Name :

</body>
</html>

Step5:
Create a Controller which will handle the greetingController.htm request.

package com.sivalabs.springmvc.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;

/**
* @author SivaLabs
*
*/
@Controller
public class GreetingController
{
@RequestMapping("/greetingController")
public ModelAndView greet(HttpServletRequest request, HttpServletResponse response)
{
ModelAndView mav = new ModelAndView("welcome");
String username = request.getParameter("username");
mav.getModel().put("LOGGEDIN_USERNAME", username);
return mav;
}
}

Step6:
Create welcome.jsp to show the welcome message and put it in /WEB-INF/jsp folder.

<html>
<head>
<title>SpringMVC - HelloWorld</title>
</head>

<body>
Welcome ${LOGGEDIN_USERNAME}
</body>
</html> 
 
For source code download at 

SpringMVCHibernate.zip

Leave a Reply

Your email address will not be published. Required fields are marked *