Mohamed Amine Ouali
Mohamed Amine Ouali

Reputation: 615

template and layout for spring-mvc application

I am using jee with spring mvc framework. I want to make a template that holds a fix menu and a content that change depending on the URI.

I want to execute same code for my template suppose that there is something to load from the database in the template. So I want a java method to be executed if the template is loaded and initialise all the dynamic content.

What is the best option to choose? Is this possible with Thymeleaf view engine? Is there another alternatives?

Thanks

Upvotes: 0

Views: 1337

Answers (1)

Louca
Louca

Reputation: 21

Ok if I get you correctly, I think Thymeleaf is the best option to choose, you can create a template page like this one:

template.html:

<!DOCTYPE html>
  <html xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

/* Here you put your CSS and JS files and so on.. */

<body>
  <header> ... </header>


    /* Here you can put anything you want beacause this template will be
 always executed */


  <section th:fragment="content></section>

  <footer> ... </footer>
</body>

You can make your own header and footer for all your HTML pages.

Then you can create your HTML pages using this template. For example if you choose to realize a homepage you can do it like this

home.html:

<!DOCTYPE html>
  <html xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
  layout:decorator="template">

 /* Here you have your CSS, JS files.. and body tag */

 <div layout:fragment="content"> ... </div>

Anything you'll put in your div will be included in the section tag of your template page.

Upvotes: 2

Related Questions