简体   繁体   中英

How to access Spring MVC model objects in Freemarker template?

New to Spring MVC and 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. But it didn't work. I am completely clueless about accessing model objects into freemarker template.
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

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

<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. Change your freemarker template to:

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

You can also do something like this 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

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

This same object can be used with the @ModelAttribue annotation too

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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