java 如何在 Freemarker 模板中访问 Spring MVC 模型对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17655964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Spring MVC model objects in Freemarker template?
提问by Himanshu Yadav
New to Spring MVC and FreeMarker framewroks. Followed thistutorial to get started with it.
When I tried to add few more model object and access it in freemarker template. But it didn't work. I am completely clueless about accessing model objects into freemarker template.
Controller
Spring MVC 和 FreeMarker 框架的新手。按照本教程开始使用它。
当我尝试添加更多模型对象并在 freemarker 模板中访问它时。但它没有用。我对将模型对象访问到 freemarker 模板完全一无所知。
控制器
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) {
String amount = "Amount";
Document xml = readProductXML();
model.addAttribute("users", userList);
model.addAttribute("sectionName",amount);
return "index";
}
Freemarker
自由标记
<#import "spring.ftl" as spring />
<fieldset>
<legend>${sectionName}</legend>
</fieldset>
</#list>
Error
错误
The following has evaluated to null or missing:
==> sectionName [in template "index.ftl" at line 28, column 67]
XML
XML
<FreeMarkerUI>
<section name="amount" label="Amount">
<field name="firstName" label="First Name" type="text" mandatory="yes"/>
<field name="lastName" label="Last Name" type="text" mandatory="yes"/>
<field name="country" label="Country" type="dropdown" codeTableName="country" mandatory="no"/>
</section>
</FreeMarkerUI>
采纳答案by Himanshu Yadav
I have got it working by changing the controller method to
我通过将控制器方法更改为
public ModelAndView home() {
ModelAndView mav = new ModelAndView();
mav.addObject("users", userList);
mav.addObject("user", "Big Joe");
return mav;
}
回答by Bram Rooijmans
You need to bind the name of the @ModelAttribute to be able to use it in a Freemarker expression. Change your freemarker template to:
您需要绑定 @ModelAttribute 的名称才能在 Freemarker 表达式中使用它。将您的 freemarker 模板更改为:
<#import "spring.ftl" as spring />
<@spring.bind "model" />
<fieldset>
<legend>${model.sectionName}</legend>
</fieldset>
回答by Kris Boyd
You can also do something like this Himanshu:
你也可以做这样的Himanshu:
public class User {
private List<String> users;
private String user;
//getters and setters
}
public ModelAndView home (){
ModelAndView mav = new ModelAndView("/someurl");
User userToReturn = new User();
userToReturn.setUser("Big Joe");
//get userList from somewhere
userToReturn.setUsers(userList);
mav.addObject("user", userToReturn);
return mav;
}
Then inside your Freemarker you can access the user object simply by doing
然后在您的 Freemarker 中,您只需执行以下操作即可访问用户对象
<fieldset>
<legend>${user.user}</legend>
</fieldset>
Or
或者
<fieldset>
<#list user.users as displayUser>
<legend>${displayUser}</legend>
</#list>
</fieldset>
You changed some of your variable names after you posted the second time, so I just combined the two posts to give the answer.
你在第二次发帖后更改了一些变量名称,所以我只是将两个帖子结合起来给出答案。
This is a very simplistic example, but if you have a lot of different parameters coming into your controller and model attributes going out, it can become a lot more useful to keep them all in one Object rather than having to define each model object for the MAV to handle.
这是一个非常简单的例子,但是如果你有很多不同的参数进入你的控制器并且模型属性输出,那么将它们全部保存在一个对象中而不是必须为每个模型对象定义每个模型对象会变得更加有用。 MAV 来处理。
This same object can be used with the @ModelAttribue
annotation too
同样的对象也可以与@ModelAttribue
注释一起使用
public ModelAndView home (@ModelAttribute User user) {
ModelAndView mav = new ModelAndView("/someurl");
//do cool and exciting stuff with the user object
mav.addObject("user", user);
return mav;
}
This can help if you want to pass in information to the controller method over and over again from the same page.
如果您想从同一页面一遍又一遍地将信息传递给控制器方法,这会有所帮助。