如何在 spring mvc 中使用带有 FreeMarker 的宏
how to use macros with FreeMarker in spring mvc
你好,我正在 spring mvc 项目中工作,我想使用 FreeMarker 作为我的模板引擎,我在使用宏时遇到问题我想创建母版页或布局(页脚、页眉和菜单)我可以在我的其他页面中使用,到目前为止我正在做一个简单的例子来说明如何做但是我不能让它工作,我放在宏中的内容没有显示我的 FreeMarker 版本是 2.3.21.
这是我的 freemarker java class 配置:
@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
有什么区别
您正在使用所谓的 "Alternative (square bracket) syntax"。看看这里:http://freemarker.org/docs/dgui_misc_alternativesyntax.html
最有可能通过在模板开头使用 ([#ftl] as stated in the documentation) 替换尖括号上的方括号或(对此不确定)来解决问题。
你好,我正在 spring mvc 项目中工作,我想使用 FreeMarker 作为我的模板引擎,我在使用宏时遇到问题我想创建母版页或布局(页脚、页眉和菜单)我可以在我的其他页面中使用,到目前为止我正在做一个简单的例子来说明如何做但是我不能让它工作,我放在宏中的内容没有显示我的 FreeMarker 版本是 2.3.21.
这是我的 freemarker java class 配置:
@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
有什么区别您正在使用所谓的 "Alternative (square bracket) syntax"。看看这里:http://freemarker.org/docs/dgui_misc_alternativesyntax.html 最有可能通过在模板开头使用 ([#ftl] as stated in the documentation) 替换尖括号上的方括号或(对此不确定)来解决问题。