0
Sponsored Links


Ad by Google
In this post we are going to start our very first Spring MVC off course Hello World implementation example step by step.
Spring Framework is a very popular open source application framework developed using Java language. Spring Framework is nowadays top in the framework list and off course high payee job and yes very easy to learn. Current stable release of Spring is 4.1.4 and we are using the same here.

If you are a J2EE developer you must have to know Spring Framework, well it has various modules and the best part of spring is modularity. What you need to do is just pick what you need and leave others so definitely light weighted because of  modularity.

OK come to the point we are here to start our Spring MVC tutorial and today is our first day first show, so start with Hello World Example you just need to follow the steps and you will complete the Hello World example and of course at last we have download able link from where you can download this Hello World example.

Tools and Technologies we are using here:

  • JDK 7
  • Spring 4.1
  • Eclipse Juno 4.2

Overview of the Project Structure:

Main Objects of this project are:
  • spring 4.1.4 jar files
  • web.xml
  • spring4demo-servlet.xml
  • HelloWorldController.java

Below are the list of spring 4 jar files we are using in this project.
  • spring-aop-4.1.4.RELEASE.jar
  • spring-beans-4.1.4.RELEASE.jar
  • spring-context-4.1.4.RELEASE.jar
  • spring-core-4.1.4.RELEASE.jar
  • spring-expression-4.1.4.RELEASE.jar
  • spring-web-4.1.4.RELEASE.jar
  • spring-webmvc-4.1.4.RELEASE.jar
  • jstl-1.2.jar
  • commons-logging-1.2.jar

From here you can read more about Front Controller in Spring MVC

Step 1: Create Dynamic Web Project
A. Go to File Menu then New->Dynamic Web Project, and provide the name of the project.
B. Provide project name see below screen shot.


C. Click Next see below screen shot.

D. Click Next and select Generate web.xml check box see from the screen shot and then Click on Finish button.


That's it. Now, after completion of all the above steps your project looks as below screen shot.


Step 2: Now double click on web.xml file and paste the below code inside <web-app>here</web-app> tag.

<servlet>    
   <!-- spring4demo servlet name can be anything -->
  <servlet-name>spring4demo</servlet-name>
  <!-- DispatcherServlet Known as front controller in spring -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>    
 </servlet>    
 <servlet-mapping>    
  <servlet-name>spring4demo</servlet-name>    
  <url-pattern>*.htm</url-pattern>    
 </servlet-mapping> 

Here is a complete web.xml
<?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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring4_MVC_Hello_World_Example</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>    
   <!-- spring4demo servlet name can be anything -->
  <servlet-name>spring4demo</servlet-name>
  <!-- DispatcherServlet Known as front controller in spring -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>    
 </servlet>    
 <servlet-mapping>    
  <servlet-name>spring4demo</servlet-name>    
  <url-pattern>*.htm</url-pattern>    
 </servlet-mapping>  
</web-app>

Step 3: Create spring4demo-servlet.xml paralel to web.xml and place the below code.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 <context:component-scan base-package="com.javamakeuse.spring.helloworld" />

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
</beans>  

Step 4: Create index.jsp Right click on WebContent folder then New->JSP File and provide name as index.jsp

Here is a complete index.jsp page place this code in your index.jsp page.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC Hello World Example</title>
</head>
<body>
 <center>
  Click <a href="printHello.htm">here</a> to call spring controller.
 </center>
</body>
</html>

Step 5: Download and Place all the required jar files(listed above) inside lib folder(WebContent->WEB-INF->lib)

Note: Download this project, you will get complete jar files.

Step 6: Create a new folder named it as pages inside WebContent->WEB-INF->pages and create another jsp file helloworld.jsp inside this folder.
Here is a complete helloworld.jsp file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
 <center>${msg}</center>
</body>
</html>

Step 7: Create HelloWorldController.java In Spring any class can be a controller if that class is uses @Controller above the class. Below is the complete HelloWorldController.java class.
package com.javamakeuse.spring.helloworld;

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

@Controller
public class HelloWorldController {

 @RequestMapping(value = "printHello")
 public ModelAndView returnHelloWorld() {
  System.out
    .println("Controller is called going to return helloworld.jsp");
  
  String dynamicMessage = "You did it !!!";
  
  return new ModelAndView("helloworld", "msg", dynamicMessage);
 }
}

Finally Right click on Project and select Run As->Run On Server

URL: http://localhost:8080/Spring4_MVC_Hello_World_Example/

Click on the link and here is another page

That's it.
While learning Spring MVC most of us getting java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet exception if you are also facing the same here is the fix of this exception

Download the complete example from here Source Code+lib


References:
Reference 1
Reference 2

Sponsored Links

0 comments:

Post a Comment