【问题标题】:How to access request/params, etc. with Spring Boot and Groovy templates如何使用 Spring Boot 和 Groovy 模板访问请求/参数等
【发布时间】:2014-11-09 23:44:59
【问题描述】:

我有一个 Groovy 模板 login.tpl 文件,类似于:

layout 'layout.tpl', title: 'LOGIN',
        content: contents {
//          if(param.logout) p class: "alert", "You have been logged out"
//          if(param.error) p class: "alert alert-error", "There was an error, please try again"
            form id: 'loginForm', class: 'form-horizontal',
                    role: 'form', action: '/login', method: 'post', {
                // BLAH here
            }
        }

这基本上是从使用 Thymeleaf 的 Spring Boot sample-web-secure starter 版本翻译而来的。

如何访问请求/响应/会话等内在函数?我是否需要破解一个中间控制器来手动将这些东西放入模型中?

【问题讨论】:

    标签: groovy spring-boot template-engine


    【解决方案1】:

    Spring 将RequestContext 暴露给模板视图(参见AbstractTemplateView)。在 Spring Boot 1.1 中,它被称为“spring”。在 1.2 中,它使用 Spring MVC 的默认名称:“springMacroRequestContext”。

    【讨论】:

    • 其实我接受的太早了。试过'p“$ {springMacroRequestContext}”'它返回null。是否有任何示例代码来展示如何在 Groovy 模板中使用它?
    • 我试过 'p "${spring.request.serverName}"' 并且成功了。使用 1.2.0.M2。
    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2019-12-26
    • 2021-09-16
    相关资源
    最近更新 更多