javaspringspring-mvcfreemarker

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>

Solution

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