Objectives
- Update the project dependencies to work with the Thymeleaf templating engine.
- Prepare packages and necessary files for the web interface within the project structure.
- Create a web user interface.
Motivation
The goal of this exercise is to create a web user interface for your game using the Thymeleaf templating engine and the MVC pattern.
The implementation includes static resources, dynamic templates (Thymeleaf), and a controller for the graphical game component (Spring MVC).
Instructions
Step 1
Task 1.1
In the Maven configuration file pom.xml, add a new dependency that enables work with the Thymeleaf templating engine.
Add the following to the existing project dependencies (dependencies):
<dependencies>
... //keep the previously added dependencies unchanged
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
Comment
An active internet connection is required when adding the dependency so that the necessary libraries can be downloaded. In the IDE, a Maven notification may appear asking you to confirm the dependency update. This can also be triggered manually via the context menu (right-click) Maven -> Reload project or through the Maven tool window using Reload All Maven Projects.
Step 2
For the web interface to work via Thymeleaf templates, a controller and resources that make up the web interface are needed.
Task 2.1
Add directories to the project for organizing the resources used to build the web interface.
Resources for the web interface, such as static HTML files, CSS, images, and templates for Thymeleaf, will be placed in the resources directory as follows:
src
└── main
└── resources
├── static
│ ├── css
│ │ └── *.css
│ ├── images
│ │ ├── *.png
│ │ └── *.svg // images in .svg or .png format)
│ └── index.html
├── templates
│ ├── fragments // optional package for Thymeleaf template fragments
│ │ └── *.html
│ ├── *.html
└── application.properties
Task 2.2
Add a package to the project that will contain the controller implementation. In this package, create a controller class for your game.
The controller for the Thymeleaf template is a separate class used to process and manage URL parameters and URL routing.
Controllers will be stored on the server side in the package sk.tuke.gamestudio.server.controller. Name the controller class according to your game, for example for the game Mines it would be MinesController.
src
└── main
└── java
└── sk.tuke.gamestudio
└── server
└── controller
└── {Nazov}Controller.java
- Controller classes are annotated with
@Controllerand@Scope(WebApplicationContext.SCOPE_SESSION). - Controller methods are mapped to corresponding URLs using the
@RequestMappingannotation. There may be more than one such method in a controller. - Method parameters may be annotated with
@RequestParam, which is used to obtain URL parameters. The method also includes a parameter of typeModel, representing the context for the template being prepared. We can add attributes to theModelobject that we need to be available in the template. The return value is the name of the view (Thymeleaf template) that should be displayed after processing the parameters.
An example of a method mapped to a URL can be seen in the mines() method of the Minesweeper controller:
@RequestMapping("/mines")
public String mines(@RequestParam(value = "command", required = false) String command,
@RequestParam(value = "row", required = false) String row,
@RequestParam(value = "column", required = false) String column,
Model model) {
// if required, add additional code, e.g. to check provided parameters for null
...
return "mines"; //same name as the template
}
The mines() method is called when a page is requested at the relative address specified in the @RequestMapping annotation. URL parameters may also be included, as listed in the @RequestParam annotations.
When calling the mines() method, a prepared Model object is also passed. We can add attributes to this model that we want to be available within the HTML template for Thymeleaf and that help us create the current page content. The template with the name returned by the method, in this case "mines", is used to generate the HTML page that the server provides for display in the web browser.
Step 3
Task 3.1
In the templates directory, create a Thymeleaf template representing the graphical appearance of your game component in HTML view.
Comment
Get inspired by the example for the Minesweeper game.
An example of a template for the Minesweeper game contains the following parts:
- Graphical representation of the Minesweeper game component,
<div th:utext="${@minesController.getHtmlField}"/>
- Buttons for controlling the game,
<a href="/mines/new">New game</a><br/>
<a href="/mines/mark" th:text='${@minesController.marking ? "Mark" : "Open"}'/><br/>
- Display of the top scores table, which can be implemented in several ways. You can choose the one that suits you best, for example:
- a table generated on the server using the Thymeleaf system, using a JPA service,
- a table generated on the client side using AJAX calls and a REST service,
- a table generated on the client side using jQuery DataTable and a REST service.
Comment
When implementing the graphical appearance of the game component, you may be inspired by the GameStudio project from the lecture. You can also draw inspiration from the alternative project implementation, which uses Thymeleaf elements and CSS styling to build the web interface.
Task 3.2
Implement the display for the remaining services:
- list of comments in a table,
- form for submitting the player's comment to the game,
- display of the game's average rating (as a number),
- display of the player's rating of the game,
- form or buttons (stars) for submitting or updating the player's rating.
Use the services implemented in previous exercises using JPA or REST, also in the following steps. Adapt the code as needed for your project and ensure the services are available to the player in the game.
Task 3.3
Run the server project and open the game in the browser at http://localhost:8080. Test the functionality of your game.
Verify that all service components work and that the added and displayed records are correct.
Step 4
Task 4.1
Upload (commit and push) your implementation to your GitLab repository. You can continue updating the project gradually. Before the next exercise, make sure your latest files are in the repository. Also prepare questions you would like to discuss in the next exercise.
Resources
- GameStudio project (GitLab) from the lectures
- Alternative GameStudio project using Thymeleaf elements and CSS
- Thymeleaf templating engine documentation
- Working with Fragments in Thymeleaf
- Thymeleaf Page Layouts
- CSS Flexbox in a playful way: Flexbox Froggy
- A systematic approach to naming CSS identifiers: BEM Naming
- jQuery Datatables For Beginners
- JS Array Data Source