Spring 4 - What's new & Simple Hello World Example Tutorial

Today, where spring can be found as ultimate solution in java world for not only in web applications but almost every aspect in IT industry. We have discussed a number of topics under spring tutorial series. Spring has launched its latest version as Spring 4 ,I was going through the new release and found it very useful and a advancement of spring.

I downloaded the new release that is not yet finalized till the blog is being published. 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.

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.


Now i will explain you step by step configuration and class files importance.


WebContent\WEB-INF\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_2_5.xsd"  
 id="WebApp_ID" version="2.5">  
 <display-name>Spring4MVC</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> 



WebContent\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>  
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>  
 <title>Being Java Guys | Hello World</title>  
</head>  
<body>  
 <center>  
 <h2>Being Java Guys | Hello World</h2>  
 <h4><a href="hello.html">Click Here</a></h4>  
 </center>  
</body>  
</html>



WebContent\WEB-INF\spring-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: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.beingjavaguys.controller" />  
  
 <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> 



src\com\beingjavaguys\controller\HomeController.java

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.
package com.beingjavaguys.controller;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
public class HomeController {  
  
 @RequestMapping("/hello")  
 public ModelAndView test() {  
  String message = "Welcome to Spring 4.0 !";  
  return new ModelAndView("hello", "message", message);  
 }  
}  



WebContent\WEB-INF\pages\hello.jsp

This is nothing but a simple jsp file, once the request is completed the control is moved to a jsp file.
<html>  
<head>  
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>  
<title>Being Java Guys | Hello World</title>  
</head>  
<body>  
 <center>  
  <h2>Being Java Guys | Hello World</h2>  
  <h4>${message}</h4>  
 </center>  
</body>  
</html> 



Data flow of Spring MVC web application.

As the request comes to container it goes to web.xml, we have added a spring entry there this pushes the request to spring-servlet. Here the base package is read by the container and it searches all java classes with '@Controller' annotation in the package. Than an appropriate method is being called having same url as the requested one. Code written in method is executed line by line and an output is generated and showed on browsed as per the view resolver configuration.


In this particular blog we cam across 'Spring hello world application' and a detailed explanation to each and every configuration file in the project setup. In upcoming blogs we will cover other topics related to spring and other technologies.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.