Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Monday, 3 June 2013

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.









Thanks for reading !
Being Java Guys Team




9 comments:

  1. Awesome Nagesh, Looks very simple, need to give it a try.
    A quick question, best pdf for complete springs4 tutorial ??

    ReplyDelete
  2. The tutorial is nice, It would have been better if you compare spring3 and 4

    ReplyDelete
  3. whats new in spring 4

    ReplyDelete
  4. I don't see any new feature of Spring 4 in this example

    ReplyDelete
  5. Build is failing in Eclipse Kepler, with Register Form
    2014-03-24 16:16:33.279:WARN:oejs.ServletHandler:
    org.apache.jasper.JasperException: /WEB-INF/view/Register.jsp(4,61) PWC6188: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application on Windows 7. same code is working on Windows XP machine...can you pls check!

    ReplyDelete
    Replies
    1. For Windows 7 64 bit the issue was solved by unzipping jstl.jar, and copying the c.tld file under WEB-INF/view/tld and specifying <%@ taglib uri="tld/c.tld" prefix="c"%> in both Register.jsp and UserDetails.jsp. Till someone suggests a better solution, this works for me

      Delete
  6. How do you run this example on JBoss 7.1.1?
    I created a war called spring4 and deployed it. But localhost:8081/spring4 gives 404. the war looks deployed on the admin console.

    ReplyDelete
    Replies
    1. You at least need to add /hello.html to your request. Your call expects an index.jsp to be present in the /webcontent directory

      Delete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft