Skip to content

02 The First Controller

Julia Damerow edited this page Feb 18, 2022 · 1 revision

Spring Controllers

In a Spring MVC project, controllers are responsible for handling incoming requests and calling service classes. They use Model objects to pass data to the view, which in this tutorial is implemented using Thymeleaf. This means that for every page of the web application you should find a controller providing the information needed for a page. In the base project, there is one controller in the package edu.asu.diging.springaction.web called HomeController:

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String home(Model model) {
        return "home";
    }
}

Note the two annotations @Controller and @RequestMapping. The Controller annotation tells Spring that this class is a controller. The RequestMapping annotation tells Spring that the method home should be called when the root path (/) of the application is requested (in our cases when you go to http://localhost:8080/spring-to-action/).

Inside the home method, you can add new attributes to the model variable, which will then be available in the Thymeleaf template. Try adding a new attribute "currentDate" that holds the current date.

@RequestMapping(value = "/")
public String home(Model model) {
    model.addAttribute("currentDate", new Date());
    return "home";
}

Thymeleaf Template

Now, that we've added the current date to the model to be passed to the frontend, let's make sure it's shown on the page. First, we need to find the template used to create the page. The home method returns a string (home), Thymeleaf will prefix this string with /WEB-INF/views/ and append .html to find the template, which means we can find the template in /WEB-INF/views/home.html. Prefix and suffix used to build the template locations can be changed in the Thymeleaf configuration class (edu.asu.diging.springaction.config.ThymeleafConfig).

Open the template. You should see this:

<html layout:decorate="~{layouts/main}">
  <head>
    <title>Authority Matcher</title>
  </head>
  <body>
   <div layout:fragment="content">
      <div class="jumbotron col-md-12">

	<h1>Congratulation!</h1>
	<p>
	   You did it. This basic webapp is all set up now. Try to login as "admin" with password "admin".
	</p>

      </div>
   </div>
  </body>
</html>

The base project is setup to use layouts, this means the final page will be a combination of the layout defined in layouts/main (see line one of the template) and the div that has the attribute (layout:fragment="content") in home.html.

Try adding <p th:text="${currentDate}" ></p> to the template after the paragraph:

<div class="jumbotron col-md-12">
   <h1>Congratulation!</h1>
   <p>
	You did it. This basic webapp is all set up now. Try to login as "admin" with password "admin".
   </p>
   <p th:text="${currentDate}" ></p>
</div>

Reload the browser page of you application. You should now see the current date and time. The attribute th:text="${currentDate}" tells Thymeleaf to print the model attribute currentDate within the paragraph. You can put any type of object into model attributes and then access their properties using the dot notation, e.g. assume you have a class Book:

class Book {
   private String title;

   public Book(String title) {
      this.title = title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getTitle() {
      return this.title;
   }
}

If you put a Book object into the model in a controller (model.addAttribute("book", new Book("My Book"))), you can access its title in the template like this: <span th:text="{book.title}" />.

Tips & Tricks

Hot Deploy

By default Tomcat hot deploys an application in Eclipse. This means that if you change a Java file and then save it, you do not need to restart Tomcat. Tomcat will notice that the file has changed and automatically redeploy the application. You can change this behavior by double-clicking on your Tomcat server in the "Servers" tab. A new editor with Tomcat's settings will open. You can change hot deploy in the "Publishing" section. You can also change the timeout time for startup and shutdown. Make sure to save the editor if you make changes and then restart Tomcat.

Clone this wiki locally