1

I'm learning scalate template engine. How can I pass an object (for example User) from my controller to template .ssp in my scalate template ?

my controller

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! the client locale is "+ locale.toString());

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    User user = new User("Dawid", "Pacholczyk");

    model.addAttribute(user);

    return "defaultTemplate";
}
Fixus
  • 4,631
  • 10
  • 38
  • 67

1 Answers1

1

Given the Spring support is implemented using a ViewResolver I guess you can pass parameters to it like this:

    val response = new ModelAndView
    response.addObject("user", new User)
    return response

Have a look at the spring example as well.

Edit:

You need to return a ModelAndView like so:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
    ...
    User user = new User("Dawid", "Pacholczyk");
    template = new ModelAndView("defaultTemplate");
    template.addObject("user", user);
    return template;
}
ebaxt
  • 8,287
  • 1
  • 34
  • 36
  • thank you for reponse. I've edited my post and I posted my controller. As you can see I'm returning the template name so I don't know how to combine this with your method :( – Fixus Mar 17 '12 at 18:57
  • you`re awsome. You solved my problem with template. BUT I still get exception :/ org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.fusesource.scalate.CompilerException: Compilation failed: /WEB-INF/scalate/defaultTemplate.ssp:3.13 error: not found: type User <%@ var user: User %> – Fixus Mar 17 '12 at 19:07
  • 1
    Not sure about that one, I would suggest you post another question with the template, spring configuration code and the controller logic. – ebaxt Mar 17 '12 at 19:19