Skip to content

Top Songs and Spring MVC

joecrean edited this page Aug 21, 2012 · 11 revisions

Introduction

Spring MVC is a mix between the popular Spring framework and the MVC pattern for building user facing applications (used across many programming languages).

In detail Top Songs uses the following components

  • Servlets (Java Enterprise Edition or JEE)
  • Spring MVC (in implementing the Servlets)
  • Java Server Pages (JSPs) in implementing the presentation layer. These make use of the JSTL tag language.
  • MarkLogic Java API (in retrieving the model data from the database).

Top Songs is built as a Java Web Application and deployed into Tomcat 7.

Spring MVC

The main usefulness of Spring MVC is to provide a series of Java annotations which considerably reduce the amount of boilerplate code in a Servlet. These annotations can be used to map Java classes and methods to URIs as well as provide access to parameters from the URL. Here is a simple tutorial in Spring MVC.

As outlined here Spring MVC uses the notion of a Front Controller (implemented as the Spring DispatcherServlet) which dispatches HTTP requests to application controllers (or servlets) according to rules specified in Spring configuration.

In Top Songs, annotations are used in the DefaultController.java and SearchController.java classes. Top Songs features the following annotations:

  • @Controller signifies that the corresponding class is to be used as a controller
  • @RequestMapping("/foo") maps all requests whose URLs begin with /foo to this class/method e.g. @RequestMapping("/search") is defined for the SearchController class and @RequestMapping("detail.html") for the detail() method. In this way a request for the URI /search/detail.html is mapped to the detail() method of the SearchController class.
  • @RequestParam(required=true, value="uri") extracts a parameter called uri and its value from the URL, throws an exception if it is not present.

The DispatcherServlet is defined in the web.xml file and its specific configuration can be found in dispatcher-servlet.xml. This last xml file determines where and how JSP files are to be found (in our case they are defined in /WEB-INF/jsp and have a jsp suffix so that the string "search" will map to /WEB-INF/jsp/search.jsp. It also specifies the path to resources like css and image files.

With this simple configuration it is very easy to create a basic servlet and its mapping to various URIs.

Model Data and Presentation

Once a method in a controller has received control it will, generally speaking, validate user input, retrieve data from the data source and make it available for presentation. Each method in SearchController.java which maps to a URI component also has an extra parameter of type Model. This is required for passing data to the presentation layer or view (the "V" from MVC). In our application the view is implemented by Java Server Pages (JSPs). So, the controller will process the user action, retrieve the data and then add it to the model object. The Spring MVC framework will route control to the JSP (specified in the string returned by the controller method) which will read the data provided in the model object and use it to build the page to be displayed.

As an example take the last few lines of the search() method in SearchController(): notice that data is being added to the model object which be later available to a JSP. The method is returning a string containing the text search, the Spring MVC framework will map this to a JSP called search.jsp.

	model.addAttribute("mode", "list");
	model.addAttribute("results", results);
	model.addAttribute("query", query);
	model.addAttribute("sortoptions", options);
	model.addAttribute("page", pagination);
	if (credentials.isLoggedOn()) {
		model.addAttribute("login", "ok");
		model.addAttribute("loginmsg", " you are logged in as "+credentials.getCurrentLevel());
	}
	return "search";

Top Songs URI Mapping

This section will discuss the URI mapping for the Top Songs application. For the purposes of this discussion we shall assume the base URI of all URIs mentioned is http://host:port/TopSongs.

/ DefaultRouteController.java : this redirects to /search/search.html
/search SearchController.java
/search/search.html search() : performs search
/search/detail.html detail() : retrieves song details
/search/image serveImage() : serves binary images (album artwork)
/search/advanced.html advanced() : displays advanced search form
/search/advancedSearch.html advancedSearch() : processes advanced search form and triggers search
/search/autocomplete.html autocompleteSearch() : returns search suggestions for title autocompletion on the advanced search page. This is invoked by JavaScript on that page.
/search/login.html login() : simulates a login
/search/logout.html logout() : simulates a login
/search/insertform.html insertForm() : displays the form used to insert a new song to the database
/search/insertsong.html insertSong() : processes the above form
/search/bday.html birthdaySearch() : retrieves songs dating from or near the users birthdate.