繁体   English   中英

如何在Spring MVC中将宏与FreeMarker一起使用

[英]how to use macros with FreeMarker in spring mvc

嗨,我在spring mvc项目中工作,我想使用FreeMarker作为我的模板引擎,我在使用宏时遇到问题,我想用(页脚,页眉和菜单)创建母版页或布局在我的其他页面中使用,到目前为止,我正在做一个简单的示例,但我无法使其正常工作,我在宏中放入的内容未显示我的FreeMarker版本为2.3.21。

这是我的freemarker java类配置:

 @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;
 }

我将我的宏另存为.html页面,例如myMacro.html

这是myMacro.html文件

<#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>

这是我称为宏的页面

myPage.html下

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

我的footer.html

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

我的header.html

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

我的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>  

这是我访问myPage.html时得到的输出

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

这是我的控制器

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



        return "myPage";
    }

编辑:我在导入布局时使用的是和其他语法:

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

但我这样编辑,它有效

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

但是我还有另一个问题,我是freemarker的新手,我不知道如果将它们另存为.html或.ftl有什么区别?

您正在使用所谓的“替代(方括号)语法”。 在这里看看: http : //freemarker.org/docs/dgui_misc_alternativesyntax.html最有可能通过替换尖括号上的方括号解决问题,或者通过使用([#ftl]文档)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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