Home > Software Development > Practical Spring MVC Part 1: The Basics

Practical Spring MVC Part 1: The Basics

In this fast-paced, demo-driven series, I will take you on an thrilling tour of Spring MVC.  As opposed to “pet clinic” style demonstrations, I will make use of sensible solutions to genuine-globe issues in order to demonstrate the breadth of functionality supplied by Spring MVC.  This article is a perfect match for anyone seeking for a fast overview of Spring MVC and its capabilities.  Moreover, total code is included for those who want to a stick to along closely and explore the concepts presented.

Introduction

Throughout this series I will construct a fundamental blogging application that enables customers to post articles along with photos.  The source code for this project can be found here.  You can download a copy and run it from maven, or within your favourite IDE.  If you are just interested in a quick overview of the capabilities of Spring MVC feel free of charge to skip to the subsequent section.  Nevertheless, if you are interested in following along to learn more about this remarkable technologies, then let me introduce you to the project structure.

This project utilizes maven for construct and dependency management, JSR 303 for validation, JPA for object/relational mapping, and Spring for just about every thing else.  For the presentation layer I chose to use Freemarker.  The syntax is close adequate that it ought to not confuse JSP customers.  And, it offers me an chance to present to you a template technology that is in numerous techniques superior to JSP.  But, promoting you on Freemarker is not the objective of this post.

Finally, take a couple of minutes to explore the project structure.  In the top-level package you will find Article, which is annotated to help persistence, validation, as nicely as JSON and XML conversion.  Do not be concerned too significantly about these annotations.  They are not vital to understanding Spring MVC.  I have also developed ArticleRepository which provides persistence operations for Articles and ImageRepository which gives persistence operations for pictures.  These repositories are hidden behind ArticleService which offers basic services employed by the controller.

List

I will start by creating ArticleController.  Any class can be made into a controller by simply annotating it with @Controller.

@Controller
public class ArticleController {
	@Autowired
	private ArticleService service;

}

The first method will list all the articles.

@RequestMapping(value="articles", method=RequestMethod.GET)
@Transactional(readOnly=true)
public String list(ModelMap model) {
	List<Article> articles = service.find();
	model.put("articles", articles);
	return "articles/list";
}

The @RequestMapping annotation maps a URL to a controller technique.  Here we map /articles.html to the list approach.  It also enables us to specify the request technique in this case GET.  In fact, Spring makes it really simple to create a REST-complete site.

@Transactional marks the method as getting run within a transaction.  Spring will automatically commence a transaction at the beginning of the method and commit it the end of the approach.  If a runtime exception is thrown the transaction will automatically be rolled back.  You can use @Transactional on any method.  In this tutorial I have selected to annotate controller methods with transactions, which offers a excellent balance among handle and overall performance.  Ultimately, in this particular approach I have marked the transaction as read-only since we are not updating the database.

The list approach accepts a single parameter: a ModelMap.  Controller techniques can have a wide selection of parameters.  Spring will simply “do what you mean” by supplying you an “appropriate” instance.  In this example, model maps allow you to retrieve objects from and add objects to the model that will be exposed to your presentation layer (e.g. JSP or Freemarker).  Right here, we use the ArticleService to retrieve a list of articles and place that list in the model.

Spring controller methods might also return a assortment of distinct types.  Once again, Spring will try to “do what you mean”.  While we will explore some alternative return varieties later, most of our techniques will return Strings.  The string returned will be converted to a view.  Precisely how this is carried out is completely configurable.  For this particular demo, the string returned will be prefixed by /Web-INF/views/and suffixed by .FTL.  The resulting file is then evaluated as a Freemarker template.  The same method can be employed for JSP, Velocity, and any other technology.

Let us take a look at /Internet-INF/views/articles/list.ftl.  Spring MVC offers a number of really beneficial tag libraries.  The core library provides the message tag which permits us to externalized and internationalize the text in our web application.

[@spring.message code="title.welcome"/]

Take note of the Freemarker syntax.  The corresponding JSP syntax would be:

<spring:message code="title.welcome"/>

Here, the message code “title.welcome” will be translated using the property files found in /WEB-INF/lang.  The particular language file use is based on the locale, and Spring uses the browser’s request headers to determine the locale.  So, if you reconfigure your web browser to German, Hebrew, or Chinese and refresh the page you would see a different welcome message.  Theoretically, all of the text in an application should be externalized.  However, I chose not to in order to keep the demo simple.

Spring also offers a powerful URL tag which is comparable to the one found in JSP.

[@spring.url value='/articles/${article.id}.html'/]

Filter

Next, we will modify the list method so that the user can filter the list with the string query.

@RequestMapping(value="articles", method=RequestMethod.GET)
@Transactional(readOnly=true)
public String list(@RequestParam(defaultValue="") String query, ModelMap model) {
	List<Article> articles = service.find(query);
	model.put("articles", articles);
	model.put("query", query);
	return "articles/list";
}

The @RequestParam annotation maps a controller parameter to a request parameter.  By default, Spring utilizes the name of the approach parameter to figure out which request parameter to use.  So, in this instance, our parameter will be bound to a request parameter named “query”.  By default, all request parameters are needed.  Nonetheless, given that a blank query indicates that we ought to show all articles we require to make the query parameter optional by specifying a default worth (i.e. an empty string).

Note that we add the query to the model.  This is so that it can be incorporated in the query text box (so the user knows what query is at present active).  Spring does not automatically add request parameters to the model.

If the user enters “foo” in the query box, then only articles with “foo” in their title will be listed.  But, what if the user varieties “foo ” (with a space following the word)?  The query would almost certainly be more restrictive than the user would count on.  Ideally, from a usability standpoint, we probably want to trim the query string just before we use it.  Nevertheless, as a general rule we may well want to trim all string inputs.  Luckily, Spring provides an extensible service for managing the conversion of request parameter strings to arbitrary objects: the conversion service.

As an instance I created a StringTrimmerConverter.  This is registered in the Spring configuration file.  Now, any time Spring wants to convert a request parameter to a string it will use our converter which will trim it.  The conversion service can be used to support the binding of request parameters to any arbitrary datatype.  However, Spring supports most frequent information types (including numbers and dates) out-of-the-box.

View

The next method will display the details of an article.  We map this method to /articles/ID.html, where ID is the article ID.  For example, if we want to view the first article, we would use the URL /articles/1.html.  By using the brace notation in the @RequestMapping, we create a variable placeholder, which we can reference in our parameter list.

@RequestMapping(value="articles/{id}", method=RequestMethod.GET)
@Transactional(readOnly=true)
public String view(@PathVariable long id, ModelMap model) {
	Article article = service.load(id);
	model.put("article", article);
	return "articles/view";
}

We mark the first parameter, id, with @PathVariable.  Spring will match the name of the parameter to a variable placeholder in the request mapping.  In this method id will be the id of the article requested.  Placeholders are critical to implementing flexible URLs, especially REST-style URLs.

Summary

In this first component, we have noticed only the most fundamental features in Spring MVC.  However, they should give you a taste of what Spring MVC can offer you.  It facilitates the rapid creation of effective controllers without dependence on a hierarchy of framework classes or even XML.  Spring MVC makes writing controllers simple for the first time in the history of enterprise Java.

In the subsequent write-up I will demonstrate type processing an image uploading.

 

  1. No comments yet.