Spring MVC

In this tutorial, we will develop simple application using spring 3.x  mvc

Before you start development make sure below software available in your system

1)JDK 1.5 ( or above version)
2)Tomcat 5 ( or above) - here we are using tomcat 7
3)Spring 3.0.5-Release
4)Eclipse

Step 1: - Create Dynamic web project and Target runtime using Eclipse as below
File-> New -> Dynamic web project
Enter project Name and select the Target runtime ( here I using choosing Tomcat7.0) and browse to tomcat home directory.


Select the context root,Content directory and select the "Generate web.xml deployment descriptor" check box.

Step 2: Add Spring 3.0 dependencies to project
Add following jar files to project build path and copy to lib folder.

commons-logging-1.1.1.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
spring-webmvc-3.0.5.RELEASE.jar

if you are using maven, add dependecies to pom file as below
 

  3.0.5.RELEASE
 
 
 
  
  
   org.springframework
   spring-web
   ${org.springframework.version}
  
 
  
  org.springframework
  spring-core
  ${org.springframework.version}
 

  org.springframework
  spring-context
  ${org.springframework.version}
 


   org.springframework
   spring-beans
   ${org.springframework.version}


 commons-logging
 commons-logging
 ${org.springframework.version}
 


   org.springframework
   spring-asm
   ${org.springframework.version}
   


   org.springframework
   spring-expression
   ${org.springframework.version}
   


   org.springframework
   spring-webmvc
   ${org.springframework.version}
   
  
Step 3: Writing controller and mapping with annotations
  
package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/firstapplication")
public class FirstController {

 @RequestMapping(method = RequestMethod.GET)
 public String firstProgram(ModelMap model) {
 
  model.addAttribute("welcomemessage", "welcome to spring 3.0 mvc");
  return "welcome";
 
 }
}

Here we are using 2 annotations for mapping the controller with URLs

The @Controller annotation indicates that a particular class serves the role of a controller. There is no need to extend any controller base class or reference the Servlet API.
The @RequestMapping annotation is used to map URLs like '/firstapplication' onto an entire class or a particular handler method.

Step 4: Writing Views (JSP) - welcome.jsp
Simple JSP to display the message which is added in Controller
  
<html>
<body>
 <h1>${welcomemessage}</h1> 
</body>
</html>
 
Step 5: Configuration ( configure the view-resolver)
File Name : springmvcdemo-servlet.xml
 


 

 
  
   /WEB-INF/jsps/
  
  
   .jsp
  
 


Step 6: Configure the deployment descripter (web.xml)
 


  Spring MVC Demo
   
  springmvcdemo
  
                org.springframework.web.servlet.DispatcherServlet
                
  1
 
 
 
  springmvcdemo
  /
 
 
 
  contextConfigLocation
  /WEB-INF/springmvcdemo-servlet.xml
 
 
 
  
           org.springframework.web.context.ContextLoaderListener
         
 

Step 7: Run the application on Server (tomcat)
Right click on application -> Run As -> Run on Server

Step 8: Check the appliction

URL :http://localhost:8080/SpringMVCDemo/firstapplication


Very good video on spring mvc basics :

 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...