springspring-mvcscalate

how can I pass attribute from sprint to scalate template?


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

Solution

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