Spring 4

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.

 

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/

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

Download Source Code with Libs
Spring4HelloWorld.zip

 

 

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

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago