【问题标题】:how can I pass attribute from sprint to scalate template?如何将属性从 sprint 传递到 scalate 模板?
【发布时间】:2012-03-17 18:39:48
【问题描述】:

我正在学习 scalate 模板引擎。如何将对象(例如用户)从我的控制器传递到我的 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";
}

【问题讨论】:

    标签: spring spring-mvc scalate


    【解决方案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;
    }
    

    【讨论】:

    • 感谢您的回复。我编辑了我的帖子并发布了我的控制器。如您所见,我正在返回模板名称,所以我不知道如何将其与您的方法结合使用:(
    • 你真棒。你用模板解决了我的问题。但我仍然得到异常:/ org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.fusesource.scalate.CompilerException: Compilation failed: /WEB-INF/scalate/defaultTemplate.ssp:3.13 error: not found: type User
    • 不确定那个,我建议你发布另一个关于模板、弹簧配置代码和控制器逻辑的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    相关资源
    最近更新 更多