Create your own application - Spring MVC

In this tutorial, you will learn how to use Spring MVC to develop your application’s front-end. First, make sure you started by following the create your own application tutorial.

Table of Contents

Step 1 - Identify your application routes

The first thing to do before developing your application is to think about which User Interfaces will exist, sketch some mockups for those interfaces, and identify the URLs where they will be displayed. Such URLs are the routes of your application.

These are three simple routes that will allow the list, detailed view and creation of new Bananas. The next thing to do is define the controllers that will handle such routes.

Step 2 - Define your Controllers

To define a new controller, you should create a new class, for example BananaController, annotate it with @Controller and inherit from the AbstractController:

@Controller
public class BananaController extends AbstractController {
	
  @RequestMapping(value = "/bananas", method = RequestMethod.GET)
  public String listBananas(Model model) {
    User user = userLogin();
    List<Banana> bananas = ...
    model.addAttribute("bananas", bananas);
    return "bananas/list";
  }

  ...
}

The code snippet described above defines a new controller class that can handle several request mappings. In the example, we defined a request mapping that handles URLs like http://domain.com/bananas. This means that whenever there is a web browser accessing this URL, the listBananas method will handle such request.

Step 3 - Define your Views

The final step of this development workflow is to define the view that will be displayed to the end-user of your application. As said before, these views will have access to the objects injected to the Model in the controller defined in the previous step.

An excerpt of the bananas/list.jsp could be:

...
<div class="banana-list">
  <c:forEach var="banana" items="${bananas}">
    ${banana.name}<br/>
  </c:forEach>
</div>
...