[英]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. 嗨,我在spring mvc项目中工作,我想使用FreeMarker作为我的模板引擎,我在使用宏时遇到问题,我想用(页脚,页眉和菜单)创建母版页或布局在我的其他页面中使用,到目前为止,我正在做一个简单的示例,但我无法使其正常工作,我在宏中放入的内容未显示我的FreeMarker版本为2.3.21。
here is my freemarker java class configuration: 这是我的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;
}
i'm saving my macro as a .html page like this myMacro.html 我将我的宏另存为.html页面,例如myMacro.html
here is myMacro.html file 这是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>
and this is the page where i call the macro 这是我称为宏的页面
myPage.html myPage.html下
[#import "/WEB-INF/views/myMacro.html" as layout /] [@layout.myMasterPage title="My test page"] ...content goes here... [/@layout.myMasterPage ]
my footer.html 我的footer.html
<div><h1>this is the footer</h1></di
my header.html 我的header.html
<div><h1>this is the header</h1></div>
my menu.html 我的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 这是我访问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 但是我还有另一个问题,我是freemarker的新手,我不知道如果将它们另存为.html或.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. 在这里看看: http : //freemarker.org/docs/dgui_misc_alternativesyntax.html最有可能通过替换尖括号上的方括号解决问题,或者通过使用([#ftl]文档)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.