Spring MVC Newbie Tutorial 1- Hello World (NetBeans 6.7 + GlassFish v3 Prelude)


Spring MVC Newbie Tutorial 1- Hello World


This document takes you through the basics of using NetBeans IDE to develop MVC web application. It demonstrates how to create a simple web application (uses model, view, controller design pattern), deploy it to a server, and view its presentation in a browser.

Requirement(s)


To follow this tutorial, you need the following software and resources.
NetBeans IDE – Java or Web & Java EE installation (NetBeans 6.1, 6.5 or 6.7)
Java Development Kit (JDK) – (JDK version 6 or version 5)
GlassFish application server – (GF Version 2 or Version 3 Prelude)

How Spring MVC works


Basically the same way as Struts:
Spring Struts Structure Flow

Based on the HTTP request URL, the DispatcherServlet calls the corresponding Controller.
A View is rendered and sent as HTTP response.

Spring Servlet Configuration


Let’s now create the Spring configuration file ‘WEB-INF/springmvc-servlet.xml’
(name based on the servlet name above):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
 
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>
      jsp/index.jsp
    </welcome-file>
  </welcome-file-list>
 
</web-app>

Map URL /hello_world.html to Controller HelloWorldController
Declare View Resolver: when view ‘view_name’ is called (from the Controller),
the file ‘/jsp/view_name.jsp’ will be used.

Controller


Let’s create the Controller ‘WEB-INF/src/springmvc/web/HelloWorldController.java’:

package springmvc.web;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class HelloWorldController implements Controller {
 
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
 
		String aMessage = "Hello World MVC!";
 
		ModelAndView modelAndView = new ModelAndView("hello_world");
		modelAndView.addObject("message", aMessage);
 
		return modelAndView;

This Controller calls the view ‘hello_world’, passing ‘message’ to it (like a Struts attribute).

View


Now the view: ‘jsp/hello_world.jsp’:




	<p>This is my message: ${message}</p>


Summary

Reference


Spring MVC Fast Tutorial (Apache Tomcat)

Advertisements

About ayumilove

I am Ayumilove, if you see some impersonators its not me.

7 responses to “Spring MVC Newbie Tutorial 1- Hello World (NetBeans 6.7 + GlassFish v3 Prelude)”

  1. Web Video Tutorials says :

    I love your blog. I’ve added it to my favorite bookmarks and subscribed in a reader.

    Looking forward to reading more posts by you.

    Thanks.

  2. paul says :

    awesome! i wanna share too a link i found in the web. this uses the latest release from spring-framework-3.0.5.. it is a tutorial on creating web apps using spring mvc.

    http://www.adobocode.com/spring/a-spring-web-mvc-tutorial

    hope you find it okay!

  3. Toni says :

    springmvc_servlet.xml is actually web.xml code and the real mapping in springmvc_servlet.xml is missing!

  4. sosamv says :

    I think is pretty straight forward, thanks =)

  5. quincedev says :

    As a beginner: i don’t understand how to implement this part:

    Map URL /hello_world.html to Controller HelloWorldController
    Declare View Resolver: when view ‘view_name’ is called (from the Controller),
    the file ‘/jsp/view_name.jsp’ will be used.

    tsk tsk…

  6. Kanakangi says :

    You gave the web.xml code, and called it springmvc_servlet.xml?
    why do you want to spoil another day of mine?

  7. afromogli says :

    Could you explicitly show how to do the part:

    Map URL /hello_world.html to Controller HelloWorldController
    Declare View Resolver: when view ‘view_name’ is called (from the Controller),
    the file ‘/jsp/view_name.jsp’ will be used.

    ?

%d bloggers like this: