[英]freemarker with spring - how to bind global data to a template?
I want to create a small project with spring mvc and freemarker. 我想用spring mvc和freemarker创建一个小项目。
I have created a small template and a layout: 我创建了一个小模板和一个布局:
Template: 模板:
<#import "layout/defaultLayout.ftl" as layout>
<@layout.myLayout>
<div><h1>Hello Dude</h1></div>
</@layout.myLayout>
Layout: 布局:
<#macro myLayout>
<html>
<body>
<!-- menu -->
<!-- content -->
<#nested/>
</body>
</html>
</#macro>
My question now: How could I create a dynamic value(perhaps bean data)? 现在的问题是:如何创建动态值(可能是bean数据)? I do not want to add the data in each controller action.
我不想在每个控制器操作中添加数据。 What is a good solution to put such data to a complete layout that is not specific to a controller action?
有什么好的解决方案可以将此类数据放置到一个特定于控制器动作的完整布局中?
I found my own solution. 我找到了自己的解决方案。 I need to use a ControllerAdvice annotation and a ModelAttribute annotation like this:
我需要使用ControllerAdvice注释和ModelAttribute注释,如下所示:
@ControllerAdvice
public class LayoutMenu {
@ModelAttribute("foo")
public String initMenu() {
return "Hello Bar";
}
}
This solves my problem. 这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.