简体   繁体   中英

how to use macros with FreeMarker in spring mvc

hi i'm woriking in a spring mvc project and i want to use FreeMarker as my template engine, i 'm having problems using macros i want to create a master page or a layout with (the footer, header and menu)that i could use in my other pages, so far i'm doing a simple example in how to do it but i cant get it to work, the content that i put in the macro doesnt show my FreeMarker version is 2.3.21.

here is my freemarker java class configuration:

 @Bean
 public FreeMarkerConfigurer freemarkerConfig() {
     FreeMarkerConfigurer freeMarkerConfigurer  =  new FreeMarkerConfigurer();
     freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
     freeMarkerConfigurer.setDefaultEncoding("UTF-8");
     return freeMarkerConfigurer;
 }
 @Bean
 public FreeMarkerViewResolver viewResolver() {
     FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
     viewResolver.setPrefix("");
     viewResolver.setSuffix(".html");
     viewResolver.setCache(false);   //Set to true during production
     viewResolver.setContentType("text/html;charset=UTF-8");
     return viewResolver;
 }

i'm saving my macro as a .html page like this myMacro.html

here is myMacro.html file

<#macro myMasterPage title="defaultTitle">
  <html>
  <body style="width:100%;height:100%">
      print me
      <table border="1" cellspacing="0" cellpadding="0" style="width:100%;height:100%">
        <tr>
          <td colspan="2">
            <#include "Header.html"/>
          </td>
        </tr>
        <tr>
          <td>
            <#include "Menu.html"/>
          </td>
          <td>
            <#nested/>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <#include "Footer.html"/>
          </td>
        </tr>
      </table>
    </body>
  </html>
</#macro>

and this is the page where i call the macro

myPage.html

[#import "/WEB-INF/views/myMacro.html" as layout /] [@layout.myMasterPage title="My test page"] ...content goes here... [/@layout.myMasterPage ]

my footer.html

<div><h1>this is the footer</h1></di

my header.html

<div><h1>this is the header</h1></div>

my menu.html

    <div>
  <h1>
     <ul>  
       <li>Menu item 1</li>  
       <li>Menu item 2</li>  
       <li>Menu item 3</li>  
       <li>Menu item 4</li>  
       <li>Menu item 5</li>  
       <li>Menu item 6</li>
     </ul>
  </h1>
</div>  

and this is the output that i get when i access myPage.html

[#import "/WEB-INF/views/ApruebaFreeMarker.html" as layout /] [@layout.myMasterPage title="My test page"] ...content goes here... [/@layout.myMasterPage ]

this is my controller

@RequestMapping(value = "/myPage", method = RequestMethod.GET)
    public String myPage(Model model) {
        logger.info("PAge with Macro inside");



        return "myPage";
    }

EDITED: i was using and alternative syntax when i was importing my layout like this:

[#import "/WEB-INF/views/ApruebaFreeMarker.html" as layout /] [@layout.myMasterPage title="My test page"] ...content goes here... [/@layout.myMasterPage ]

but i edited like this and it works

<#import "myMacro.html" as layout>
<@layout.myLayout>
  <div><h1>Hello Dude</h1></div>
</@layout.myLayout>

but i have another question i am new to freemarker and i dont know what is the difference if i save them as a .html or as a .ftl

You're using so called "Alternative (square bracket) syntax". Take a look here: http://freemarker.org/docs/dgui_misc_alternativesyntax.html Most likely the problem would be solved by replacing square brackets on angle brackets, or (not sure about this) by using ([#ftl] as stated in the documentation) at the beginning of your templates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM