5
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 i s 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.

Simple Hello World Example Tutorial

Embed Size (px)

Citation preview

Page 1: Simple Hello World Example Tutorial

8/11/2019 Simple Hello World Example Tutorial

http://slidepdf.com/reader/full/simple-hello-world-example-tutorial 1/5

Simple Hello World Example Tutorial

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

I downloaded the new release that is not yet finalized till the blog is being published. Ihave created a simple hello world application using spring 4 mvc. In this particular blogi will explain you guys how to create a simple helloworld application in spring usingspring 4 libraries. 

Project Structure 

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

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

Page 2: Simple Hello World Example Tutorial

8/11/2019 Simple Hello World Example Tutorial

http://slidepdf.com/reader/full/simple-hello-world-example-tutorial 2/5

WebContent\WEB-INF\web.xml 

Every web project in java starts with an web.xml file, this is an entry point forapplication. As we starts the application or deploy the application on server thecontainer 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 theDispatcherServlet here to tell the container that all other upcoming requests are goingto be handled by spring itself. From now whenever a request will come to container itwill 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 seeat running project. We have added a simple link here this will call the controllermappings 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>

Page 3: Simple Hello World Example Tutorial

8/11/2019 Simple Hello World Example Tutorial

http://slidepdf.com/reader/full/simple-hello-world-example-tutorial 3/5

 </center></body></html>

WebContent\WEB-INF\spring-servlet.xml 

This is core of all spring applications, all configurations related to spring goes here in asingle file. Here one thing to note down is that the container will detect a file as springconfiguration file if the file is ending with '-servlet' suffix. We have defined a basepackage over here that helps the application to search appropriate 'url mapping'. Everyrequested 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://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>

Page 4: Simple Hello World Example Tutorial

8/11/2019 Simple Hello World Example Tutorial

http://slidepdf.com/reader/full/simple-hello-world-example-tutorial 4/5

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 controllermethod 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;

@Controllerpublic 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 movedto 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>

Page 5: Simple Hello World Example Tutorial

8/11/2019 Simple Hello World Example Tutorial

http://slidepdf.com/reader/full/simple-hello-world-example-tutorial 5/5

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.

Thanks for reading !