Let's take a step back from this spectacular new "Hello World" and try to understand what happened inside our web application. To do this, we will retrace the journey of the HTTP request our browser sent and the response it got from the server.
The entry point of every Spring web application is the DispatcherServlet
. The following figure illustrates the Dispatcher Servlet architecture:
This is a classical HttpServlet
class that dispatches HTTP requests to HandlerMapping. A HandlerMapping is an association of resources (URLs) and Controllers.
The appropriate methods—those annotated with @RequestMapping
annotation—are then called on the Controller. In this method, the controller sets the model data and returns the view name to the dispatcher.
The DispatcherServlet
will then interrogate the ViewResolver
interface to find the corresponding implementation of the view.
In our case, the ThymeleafAutoConfiguration
class has set up the view resolver for us.
You can see in the ThymeleafProperties
class that the default prefix for our views is classpath:/templates/
the default suffix is .html
.
This means that, given the view name resultPage
, the view resolver will look in the templates directory of our classpath, looking for a file called resultPage.html
.
In our application our ViewResolver
interface is static, but more advanced implementation can return different results given the request headers or the user's locale.
The view will finally be rendered and the result written to the response.
Our first page is completely static; it does not really take advantage of the power of Spring MVC. Let's spice things up a little bit. What if the "Hello World" string, instead of being hardcoded, came from the server?
It would still be a lame "hello world" you say? Yes, but it will open up many more possibilities. Let's change our resultPage.html
file to display a message coming from the model:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>Hello thymeleaf</title> </head> <body> <span th:text="${message}">Hello html</span> </body> </html>
Then, let's modify our controller so it puts this message inside this model:
@Controller public class HelloController { @RequestMapping("/") public String hello(Model model) { model.addAttribute("message", "Hello from the controller"); return "resultPage"; } }
I know, the suspense is killing you! Let's see what http://localhost:8080
looks like.
The first thing to note is that we passed a new argument to the controller's method and that the DispatcherServlet
provided the correct object for us. There are, in fact, many objects that can be injected into the controller's methods such as HttpRequest
or HttpResponse
, the Locale
, the TimeZone
, and the Principal
, which represent an authenticated user. The full list of such objects is available in the documentation, which can be found at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-arguments.