Tuesday, October 7, 2014

Spring MVC Hello World


Spring MVC Hello World Step by Step...

Requirements:
  • Spring 3.0 
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • ApacheTomcat - 6.0.26
Step 1: Create a dynamic web project in eclipse and named as "SpringMVC".
             File à New à Dynamic Web Project












Step 2: Create the controller class "MyHelloWorldController" under the package                                  com.prem.spring.controller.


package com.prem.spring.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
public class MyHelloWorldController {
 @RequestMapping(value = "/hello", method = RequestMethod.GET)
 public String printHello(ModelMap model) {
  model.addAttribute("message", "Hello World Spring 3.0 MVC!");
  return "hello";
 }

}

NOTE : @Controller - We specified the MyHelloWorldController class as a controller. 
             @RequestMapping - Map the request value to the specific method in the controller. In this example the value is hello and the controller method  we mapped for that is printHello. 
                                                
              method = RequestMethod.GET  is not mandatory.


Step 3 : Add all the Spring and other relevant jars to WebContent\WEB-INF\lib
              The following jars I added for this example. 
      • commons-logging-1.1.jar
      • org.springframework.asm-3.0.1.jar
      • org.springframework.beans-3.0.1.jar
      • org.springframework.context-3.0.1.jar
      • org.springframework.core-3.0.1.jar
      • org.springframework.expression-3.0.1.jar
      • org.springframework.web.servlet-3.0.1.jar
      • org.springframework.web-3.0.1.jar
      • jstl.jar



Step 4 : Configure the spring in "web.xml" and "spring-servlet.xml".

              The "web.xml" file as below.


<?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"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

The "spring-servlet.xml" file as below.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.prem.spring.controller" />

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

</beans>

Step 5 : Create a view page "index.jsp" and "hello.jsp" page.

The "index.jsp" page for this example as below.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.0 Hello World - Home Page</title>
</head>
<body>
 <a href="hello.html">Show Message</a>
</body>
</html>


The "hello.jsp" page as below. In this page I used the jstl for the view the message which receive from the "MyHelloWorldController" controller.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Welcome message </title>
</head>
<body>
 Welcome message : ${message}
</body>
</html>



Now the time comes to see the result..

Step 6 : Run the project on the server what we configured. In our case we used "ApacheTomcat - 6.0.26".

The index page shows as below. 



Once you click on the show message link you will receive the page as below.




That's all folks... 


No comments:

Post a Comment