变量不在GSP标记中工作,但在普通文本中工作

Pau*_*ber 3 grails spring-security gsp

我想通过快速链接为登录用户提供编辑其用户帐户的可能性.

为此,我使用正确的GSP标记创建了一个链接,并且我想使用正确的帮助程序从Spring Security UserDetails对象传递用户ID.

问题是,当我在GSP标签中时,这就像在编辑我的用户之后,而不是在我真正需要它的地方,在id属性中.

<g:link controller="user" action="show" id="${sec.loggedInUserInfo(field: "id")}">
    Edit my User ${sec.loggedInUserInfo(field: "id")}
</g:link>
Run Code Online (Sandbox Code Playgroud)

预期:

<a href="/Backoffice/user/show/1"> Edit my User 1 </a>
Run Code Online (Sandbox Code Playgroud)

错误的结果:

<a href="/Backoffice/user/show"> Edit my User 1 </a>
Run Code Online (Sandbox Code Playgroud)

Security Tag Lib访问的UserDetails类位于:

   import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
   import org.springframework.security.core.GrantedAuthority

   class UserDetails extends GrailsUser {
       final String displayName
       final String email
       final String gravatarImage

   ...
Run Code Online (Sandbox Code Playgroud)

id在GrailsUser基类中定义为Object.

GrailsUser类扩展User {

private final Object _id

    ...
Run Code Online (Sandbox Code Playgroud)

}

并将在此处编码为HTML:

/**
 * Renders a property (specified by the 'field' attribute) from the principal.
 *
 * @attr field REQUIRED the field name
 */
def loggedInUserInfo = { attrs, body ->

    // TODO support 'var' and 'scope' and set the result instead of writing it

    String field = assertAttribute('field', attrs, 'loggedInUserInfo')

    def source
    if (springSecurityService.isLoggedIn()) {
        source = determineSource()
        for (pathElement in field.split('\\.')) {
            source = source."$pathElement"
            if (source == null) {
                break
            }
        }
    }

    if (source) {
        out << source.encodeAsHTML()
    }
    else {
        out << body()
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的是:这是有效的.但我真的想为链接使用一致的gsp语法,我想了解为什么发布在顶部的代码不起作用.

<a href="${createLink( controller : "user", action : "show", id : sec.loggedInUserInfo(field: "id"))}">Edit my User</a>
Run Code Online (Sandbox Code Playgroud)

Vic*_*nko 7

看起来像错误的引用 - 你需要逃避"内部id="...".为了简单起见,请尝试使用field: 'id'而不是field: "id".