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:
- open
HelloWorldControllerimplemented during the third lab - remove
@ResponseBodyannotation from thesayHellomethod - we're going to return view name to be rendered instead of actual HTML - add
Modelargument to thesayHellomethod - the object is injected automatically by Spring - add
fromparameter as an attribute to theModelusing itsaddAttributemethod - make the
sayHellomethod return view name (such asgreeting) rather than HTML
Next, create a new greeting.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.
Use Thymeleaf layout inheritance 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 ProjectService to the controller and use it to load a list of all created translation projects.
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 created projects in the template as HTML unordered list.
Access the resulting page at http://localhost:8080/.