Spring 4 Framework Hello World Example

In this tutorial you will learn how to develop a Spring 4 Framework Hello world example. We hope this tutorial will give you a quick start with Spring MVC development using the latest Spring 4 Release.Here first I downloaded the new release of Spring 4 Framework from here. I have created a simple hello world application using spring 4 mvc. In this particular blog I will explain you guys how to create a simple helloworld application in spring using spring 4 libraries.

Technologies Required:

  • Spring 4.0.1.RELEASE
  • JDK 1.6
  • Tomcat 7.0
  • Eclipse or STS

Project Structure
Before we start adding some controller code lets first take a look at overall project structure in eclipse. Here one thing to note down is that we never needs to add all jar files in spring distribution. I have added only necessary jars here.

 

Spring 4 Framework Hello World Example

web.xml
Every web project in java starts with an web.xml file, this is an entry point for application. As we starts the application or deploy the application on server the container searches for an web.xml file and work according to configurations.

There is nothing new in spring 4.0 regarding web.xml file, we need to register the DispatcherServlet here to tell the container that all other upcoming requests are going to be handled by spring itself. From now whenever a request will come to container it will delegate the control to spring configuration file to be processed accordingly.

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring4HelloWorld</display-name>
  <display-name>Spring4MVC</display-name>  
 <welcome-file-list>  
  <welcome-file>index.jsp</welcome-file>  
 </welcome-file-list>  
  
 <servlet>  
  <servlet-name>spring4</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  <load-on-startup>1</load-on-startup>  
 </servlet>  
 <servlet-mapping>  
  <servlet-name>spring4</servlet-name>  
  <url-pattern>*.html</url-pattern>  
 </servlet-mapping>  
</web-app>

index.jsp
We have declared an welcome file in web.xml, this is the first file that the user will see at running project. We have added a simple link here this will call the controller mappings accordingly.

<html>
<head>
 <title>dineshonjava.com | Hello Spring 4 World</title>
</head>
<body>
 <center>
 <h2>dineshonjava.com | Hello Spring 4 World</h2>
 <h4><a href="hello.html">Click Here</a></h4>
 </center>
</body>
</html>

spring4-servlet.xml
This is core of all spring applications, all configurations related to spring goes here in a single file. Here one thing to note down is that the container will detect a file as spring configuration file if the file is ending with ‘-servlet’ suffix. We have defined a base package over here that helps the application to search appropriate ‘url mapping’. Every requested url will be searched in the controller classes defined under ‘base-package’ directory and annotated with ‘@Controller’.

Spring supports a number of view types including, jsp,html,xslt,xml,freemarker etc. We have added a simple view resolved to point our jsp file to be shown as output.

<?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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 <context:component-scan base-package="com.doj.spring4.controller" />

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

HelloSpring4Controller.java

package com.doj.spring4.controller;

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

/**
 * @author Dinesh Rajput
 *
 */
@Controller
public class HelloSpring4Controller {
 
 @RequestMapping("/hello")  
  public ModelAndView sayHello() {  
   String message = "Welcome to Spring 4.0 !!! dineshonjava.com";  
   return new ModelAndView("hello", "message", message);  
  }  
}

This is a simple controller class for our application, to make a class work as controller we need to add a ‘@Controller’ annotation. To match the appropriate url with controller method add a ‘@RequestMapping’ annotation there enclosed within double cotes.

hello.jsp

<html>
<head>
 <title>dineshonjava.com | Hello Spring 4 World</title>
</head>
<body>
 <center>
  <h2>dineshonjava.com | Hello Spring 4 World</h2>
  <h4>${message}</h4>
 </center>
</body>
</html>

We can generate a War file and deploy that to a web server to test the application. In eclipse , right click the project and Click Run As web application at tomcat server. This will build the project and create a war file in the target folder. In the case of this example the file will be Spring4HelloWorld.war

Deploy this WAR file to a web server , say Tomcat and issue.

http://localhost:8080/Spring4HelloWorld/

Spring 4 Hello World Example

http://localhost:8080/Spring4HelloWorld/hello.html

Hello World Example in Spring 4

Download Source Code with Libs
Spring4HelloWorld.zip

 

 

<<Previous <<   || Index ||   >>Next >>

 

Previous
Next

One Response

  1. Anonymous February 21, 2014