The 5 minute tutorial to getting started with Spring 3 MVC

Post to Twitter

The Spring Framework is constantly evolving. Spring MVC has gotten easier and easier to work with and with the SpringSource Tool Suite it gets even easier to get a Spring MVC project up and running. Today I’ll show you how to setup a simple Spring MVC project and add some simple functionality to it.


There are a few things you’ll need to complete this tutorial:

Java JDK installed
SpringSource Tool Suite – I downloaded the installer version, not the plug-in to Eclipse

With those two items installed and ready go ahead and run the SpringSource Tool Suite and create a new project: File -> New -> Spring Template Project

Note: It might prompt you to download the template, go ahead and let it do that.

Once the project has been created we can test out the default Hello World code that is already in-place. Lets add the project to the SpringSource tc Server and then start it.

Once the SpringSource tc Server is up and running open a browser and head to this url: http://localhost:8080/EchoSpringMVC/

If everything went well you should see this:

You can leave the SpringSource tc Server running.

Open the HomeController.java file now and add the following imports:

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

Lets add a new mapping and method for the echo functionality we are going to add.

@RequestMapping(value="/echo", method=RequestMethod.GET)
public String echo(@RequestParam("input1") String input1, Model model) {
	logger.info("Calling echo");		
	String output = "Echo: " + input1;		
	model.addAttribute("output", output);
	return "echo";
}

We set the mapping to handle any GET requests to echo. The echo method takes one parameter called input1 which we simply use to complete the echo functionality. The echo is passed back to the view (which we will create next).

Your HomeController.java should look like this:

package com.giantflyingsaucer.springmvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value="/", method=RequestMethod.GET)
	public String home() {
		logger.info("Welcome home!");
		return "home";
	}
	
	@RequestMapping(value="/echo", method=RequestMethod.GET)
	public String echo(@RequestParam("input1") String input1, Model model) {
		logger.info("Calling echo");		
		String output = "Echo: " + input1;		
		model.addAttribute("output", output);
		return "echo";
	}
}

Lets add a new JSP page for our echo view. Right-click on the webapp/WEB-INF/views folder and select:
New -> Other -> Web -> JSP File

Here are the contents for the new echo.jsp file:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Echo</title>
</head>
<body>
<h1>
	<strong><c:out value="${output}"></c:out></strong>
</h1>
</body>
</html>

Save all the files and the SpringSource tc Server will reload the project automatically. When this has happened use this URL to trigger the echo: http://localhost:8080/EchoSpringMVC/echo?input1=HelloWorld

Your result should be:

You can continue on with the second part of this tutorial.

Post to Twitter

This entry was posted in Java. Bookmark the permalink.

4 Responses to The 5 minute tutorial to getting started with Spring 3 MVC

  1. Pingback: Tutorial: Working with Spring 3 MVC and HTML Forms | Giant Flying Saucer

  2. Stuart says:

    Thanks, finally something that starts at the beginning and shows how the plumbing goes together. Great!

  3. Fred says:

    Can you show me how to insert CSS to the Spring MVC template? Thanks.

  4. Tom says:

    Chad,
    Great tutorial, short and to the point!
    STS contains several template projects. I am new to Spring and STS, but familiar with enterprise java, I have a couple questions.

    If I want to persist a domain object from Spring MVC (template project) how would I configure the Spring MVC project to do provide that capability? Or should I not do that and create a project from the STS JPA template that contains my domain, and the MVC project depends on it? Is there any doc out there on what is best practice on how to package (or what projects types to use) a Spring enterprise app. Should the projects be contained in an EAR?

    Thanks,
    Tom

Comments are closed.