I am planning to create a web application using J2ee, Spring 3.0n, Freemarker and jQuery.
My question is: is there any way to create master page with header and footer and included all Javascript files so that i can directly call that master page in all my page and save time to include all js file again and again?
Same as .Net provides concept of a master page, I want to create my own master page in Freemarker.
FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.
Freemarker Template FilesSpring boot looks for the The template files under classpath:/templates/. And for the file extension, you should name the files with . ftlh suffix since Spring Boot 2.2. If you plan on changing the location or the file extension, you should use the following configurations.
To access them, you use the . variable_name syntax.
FreeMarker is a tool that generates text output based on templates. FreeMarker syntax lets you add dynamic variables to your HTML text that permit customized output. These variables provide more flexibility and customization than CRMSDK tags that are used in version 1 templates.
Basically you write a macro, let's call it masterTemplate.
[#macro masterTemplate title="defaultTitle"]
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>${title}</title>
... scripts, style sheets, meta information ...
</head>
<body>
<div id="header">...</div>
<div id="content">
[#nested /]
</div>
<div id="footer>...</div>
</body>
</html>
[/#macro]
Then, you use this macro within your pages like this:
[#import "/path/to/masterTemplate.ftl" as layout /]
[@layout.masterTemplate title="My test page"]
...content goes here...
[/@layout.masterTemplate]
You achieve some sort of decorating technique by passing all relevant data from the page as attribute to the masterTemplate: See the title attribute. In the same way you can pass additional scripts and stylesheets.
This technique is shown here: Freemarker wiki
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With