<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">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 <b><a href="http://repo.spring.io/milestone/org/springframework/spring/4.0.0.RC1/spring-framework-4.0.0.RC1-dist.zip" target="_blank" rel="noopener">Spring 4 Framework from here</a></b>. 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.</p>
<div id="ads-id" align="center"></div>
<h2><b>Technologies Required:</b></h2>
<ul style="text-align: left;">
<li>Spring 4.0.1.RELEASE</li>
<li>JDK 1.6</li>
<li>Tomcat 7.0</li>
<li>Eclipse or STS</li>
</ul>
<p>Project Structure<br />
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.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-5.png" border="0" /></div>
<p><b>web.xml</b><br />
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.</p>
<p>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.</p>
<pre class="highlight"><;?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>; 
</pre>
<p><b>index.jsp</b><br />
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.</p>
<pre class="highlight"><;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>; 
 
</pre>
<p><b>spring4-servlet.xml</b><br />
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 &#8216;-servlet&#8217; suffix. We have defined a base package over here that helps the application to search appropriate &#8216;url mapping&#8217;. Every requested url will be searched in the controller classes defined under &#8216;base-package&#8217; directory and annotated with &#8216;<i><b>@Controller&#8217;</b></i>.</p>
<p>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.</p>
<pre class="highlight"><;?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>; 
 
</pre>
<p><b>HelloSpring4Controller.java</b></p>
<pre class="highlight">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); 
 } 
} 
</pre>
<p>This is a simple controller class for our application, to make a class work as controller we need to add a <i><b>&#8216;@Controller&#8217;</b></i> annotation. To match the appropriate url with controller method add a &#8216;<i><b>@RequestMapping&#8217;</b></i> annotation there enclosed within double cotes.</p>
<p><b>hello.jsp</b></p>
<pre class="highlight"><;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>; 
 
</pre>
<p>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 <b>Spring4HelloWorld.war</b></p>
<p>Deploy this WAR file to a web server , say Tomcat and issue.</p>
<p><i><b>http://localhost:8080/Spring4HelloWorld/</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-6.png" border="0" /></div>
<p><i><b>http://localhost:8080/Spring4HelloWorld/hello.html</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-7.png" border="0" /></div>
<h2><b>Download Source Code with Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Spring4HelloWorld.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Spring4HelloWorld.zip</a></b></h2>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring MVC Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><span style="color: red;">Spring MVC Web Tutorial</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-mvc/"><span style="color: red;">MVC Design Pattern</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-dispatcherservlet-in-spring-and-its-uses/"><span style="color: red;">Spring MVC DispatcherServlet </span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC WebApplicationContext and Root Application Context</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring MVC @Controller Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestmapping-annotation-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestMapping Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/"><span style="color: red;">Spring MVC @RequestParam Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC ContextLoaderListener</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-vs-pathvariable-annotations-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestParam and @PathVariable annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-30-mvc-hello-world-example/"><span style="color: red;">Spring MVC Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-exception-handling-example/"><span style="color: red;">Spring MVC Exception Handling Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/"><span style="color: red;">Spring MVC with Hibernate CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-tiles-plugin-with-example/"><span style="color: red;">Spring MVC Tiles Plugin with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-and-interceptor-with/"><span style="color: red;">Spring MVC Interceptor with example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/integration-spring-mvc3-and-mongodb/"><span style="color: red;">Spring MVC with MongoDB CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-internationalization/"><span style="color: red;">Spring MVC Internationalization &; Localization with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/environment-setup-for-spring-framework/">Previous</a> <;<; || <a href="https://dineshonjava.com/spring-4-tutorials-step-to-new-spring/">Index </a>|| >;>;<a href="https://dineshonjava.com/restcontroller-in-spring-40-framework/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/environment-setup-for-spring-framework/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/restcontroller-in-spring-40-framework/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '234'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…