5. Template Engine - Apache FreeMarker Macro
Contents
Through the macro features provided by FreeMarker, we can easily manage some of the commonly used template code.
Add FreeMarker Property
Add FreeMarker property into application.properties:
|
|
Create Templates
Common Template
- Right click resources/templates directory: New > File
- Fill in: common.ftl
- Click “OK” button
Copy and paste the following content into common.ftl:
|
|
Index Template
- Right click resources/templates directory: New > File
- Fill in: index.ftl
- Click “OK” button
Copy and paste the following content into index.ftl:
|
|
Add Method into FreeMarkderControll Controller
Copy and paste the following content into FreeMarkerController.java:
|
|
Verify
Run the App
|
|
Choose the correspondent option and press enter.
View Home Page in the Browser
|
|
From now on, we can use the header and footer macros from the common template in any template file.
References macro, nested, return
Author Dong Chen
LastMod Sun Apr 28 2019