Views

In the welcome() handler, we returned a very simple String to the client side, which isn't what we want, since, for a web application, we usually need to send a chunk of HTML code to the browser to render a web page or send JSON data as a result of an API call. Indeed, HTML code and JSON data are all String objects passed in an HTTP response. And it is true that you can change the return value to something like the following, as an example, so that the browser will render a text in bold:

public String welcome() {
return "<strong>Hello, Welcome to Spring Boot!</strong>";
}

However, putting HTML markups inside Java code isn't a practical way to create a web application. We need to follow the MVC pattern.

In the MVC pattern, C is our controller, V is the view where we put our HTML markups, and M is the modal that contains the data that the controller creates and that the view can use to render the final output that will be sent to the client in the HTTP response.

Let's change our application to use Thymeleaf (http://www.thymeleaf.org/), a template engine, to generate HTML code based on the HTML template we define. The first thing to do is to add the dependency. Here are the changes we make to the pom.xml file:

<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

As you can see, we didn't add the Thymeleaf library directly. Instead, we add the spring-boot-starter-thymeleaf starter module so that Spring Boot will configure this view technology for us automatically.

By default, the HTML template files that Thymeleaf will look for need to be placed under the /src/main/resources/templates directory. Let's add the welcome.html file to this folder.

Here is what the welcome.html looks like:

<strong th:text="${message}"></strong>

We keep this template very simple and only display a hello message showing in bold. th:text is a Thymeleaf syntax for rendering text and ${message} is for getting the message value from the model that we will pass from MessageController.

Now, let's remove the @ResponseBody annotation from the handler method because it is not needed for the welcome() handler anymore. Spring will take the return value of the handler as the view's name and use Thymeleaf to generate the response.

Here is the change to the welcome() handler:

...
import org.springframework.ui.Model;
...
@GetMapping("/welcome")
public String welcome(Model model) {
model.addAttribute("message", "Hello, Welcome to Spring Boot!");
return "welcome";
}

As you can see, we pass in the Model instance that Spring will create for us to the welcome() method and add the message as an attribute of model, with a key "message" that matches the one we use in the template, ${message}. Now, if you restart the application, you will see a bold welcome message on the page. By now, we have added a view technology to our application.

Another way to utilize the model and view in Spring MVC is to return an instance of org.springframework.web.servlet.ModelAndView from the handler. Here is how the welcome() method appears when using ModelAndView:

@GetMapping("/welcome")
public ModelAndView welcome() {
ModelAndView mv = new ModelAndView("welcome");
mv.addObject("message", "Hello, Welcome to Spring Boot!");
return mv;
}

As you can see, we do not need to pass in a Model object to the handler method. Instead, we create a ModelAndView instance by passing the template file's name and add the data through the addObject() method. It works the same as the previous version, only in a different flavor. You can choose the one that you like.

Besides Thymeleaf, Spring also supports other view technologies, including Groovy Markup, FreeMarker, Mustache, and the traditional JSP. If you are fans of Velocity, currently, there is no out-of-the-box support for Velocity in the latest version of Spring Boot. But you can integrate Velocity with Spring MVC with a little extra work.

There are a number of limitations with JSP when Spring Boot is running with an embedded application server. It is recommended that, when using Spring Boot, JPS should be avoided. In this book, we will use Thymeleaf.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.193