简体   繁体   English

如何在Freemarker模板中访问Spring MVC模型对象?

[英]How to access Spring MVC model objects in Freemarker template?

New to Spring MVC and FreeMarker framewroks. Spring MVC和FreeMarker framewroks的新功能。 Followed this tutorial to get started with it. 按照教程开始使用它。
When I tried to add few more model object and access it in freemarker template. 当我尝试添加更多模型对象并在freemarker模板中访问它时。 But it didn't work. 但它没有用。 I am completely clueless about accessing model objects into freemarker template. 我完全不知道将模型对象访问到freemarker模板中。
Controller 调节器

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

You need to bind the name of the @ModelAttribute to be able to use it in a Freemarker expression. 您需要绑定@ModelAttribute的名称才能在Freemarker表达式中使用它。 Change your freemarker template to: 将您的freemarker模板更改为:

<#import "spring.ftl" as spring />
<@spring.bind "model" />
<fieldset>
    <legend>${model.sectionName}</legend>
</fieldset>

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. 这是一个非常简单的示例,但是如果您的控制器和模型属性中有很多不同的参数,那么将它们全部保存在一个Object中而不是必须为每个模型对象定义它们会变得更有用。 MAV处理。

This same object can be used with the @ModelAttribue annotation too 同样的对象也可以与@ModelAttribue注释一起@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. 如果您想要从同一页面反复传递信息到控制器方法,这可能会有所帮助。

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

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

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