如何将属性从sprint传递到scalate模板?

时间:2012-03-17 18:39:48

标签: spring spring-mvc scalate

我正在学习scalate模板引擎。如何将对象(例如User)从我的控制器传递到我的scalate模板中的模板.ssp?

我的控制器

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

1 个答案:

答案 0 :(得分:1)

鉴于Spring支持是使用ViewResolver实现的,我想你可以像这样传递参数:

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

同时查看spring example

编辑:

你需要像这样返回一个ModelAndView:

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