UI ‐ server side - fidransky/kiv-pia-labs GitHub Wiki
TOTD:
- learn how rendering works in Spring
- implement basic server-side rendered UI for the semester project app
In order to create server-side UI which doesn't end up as the infamous spaghetti code, every server-side framework uses several techniques to make UI authoring easier and better maintainable.
There are many template engines one can use to create server-side web apps in Java including JSP, JSF, Wicket, Thymeleaf, Freemarker, Velocity and others. They allow composition of HTML pages with possibilities of data access and business logic. To achieve this, each of them uses some implementation of an expression language (EL).
Expression languages are rather powerful, yet limited languages. In Spring, we use the power of Spring Expression Language (SpEL), by default in the following form:
${an expression}
Within a Thymeleaf template, various forms of expression language (EL) are used (source):
${an expression} // variable expressions
*{an expression} // selection variable expressions
#{an expression} // message expressions
@{an expression} // link url expressions
~{an expression} // fragment expressions
In this lab, we're going to create a simple UI for the semester project app using server-side rendering. To do that, we're going to configure our Spring app to use Thymeleaf as the template engine.
Create a new pia-labs-ui module and add Thymeleaf Spring Boot starter dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
With Thymeleaf added, use it to render greeting returned by sayHello
method of HelloWorldController
from pia-labs-ws module instead of returning HTML directly. To do that:
- copy
HelloWorldController
implemented during the third lab - remove
@ResponseBody
annotation from the class - we're going to return view name to be rendered instead of actual HTML - add
Model
argument to thesayHello
method - the object is injected automatically by Spring - add
from
parameter as an attribute to theModel
using itsaddAttribute
method - make the
sayHello
method return view name (such ashello
) rather than HTML
Next, create a new hello.html
file in src/main/resources/templates/
folder of the pia-labs-ui module.
Note
Note that the filename matches the view name returned from the controller method.
Add some basic HTML and use Thymeleaf EL to inject greeting set to the Model
.
Thymeleaf layout dialect enables layout composition so that you don't have to repeat shared parts in each template. Add nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect
dependency to the pia-labs-ui module. Since it's added to classpath now, Spring is going pick it up and configure automatically.
Use Thymeleaf layout dialect to implement base layout of the app. Optionally, add Bootstrap to make styling easier.
Next, create a new HomeController
controller class in cz.zcu.kiv.pia.labs.controller
package of the pia-labs-ui module and implement its home
method serving HTML content at root path.
Autowire DamageService
to the controller and use it to load a list of all reported damage.
Add the loaded list to Model
and make the method return view name (e.g. home
).
Create an HTML template with filename matching the returned view name in src/main/resources/templates/
directory of the pia-labs-ui module.
Render the list of reported damage in the template as HTML unordered list.
Access the resulting page at http://localhost:8080/.