变量在GSP标签中不工作,但在正常文本中工作



我想给登录用户的可能性编辑他的用户帐户与快速链接。

为此,我使用正确的GSP标签创建了一个链接,并且我想通过Spring Security UserDetails对象传递用户Id,使用正确的Helper。

问题是,这是有效的,当我在GSP标签,就像编辑我的用户之后,但不是我真正需要它的地方,在id属性。

<g:link controller="user" action="show" id="${sec.loggedInUserInfo(field: "id")}">
    Edit my User ${sec.loggedInUserInfo(field: "id")}
</g:link>
预期:

<a href="/Backoffice/user/show/1"> Edit my User 1 </a>

错误的结果:

<a href="/Backoffice/user/show"> Edit my User 1 </a>

安全标签库正在访问的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
   ...

id在GrailsUser基类中定义为Object。

类GrailsUser扩展用户{

private final Object _id
    ...

}

并将在这里编码为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()
    }
}

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

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

看起来引用错误-您需要在id="..."中转义"。为了保持简单,请尝试使用field: 'id'而不是field: "id"

您需要将id作为参数传递,您只是将id分配给您的链接…

<g:link controller="user" action="show" params="[id:${sec.loggedInUserInfo(field: "id")}]" id="${sec.loggedInUserInfo(field: "id")}">
    Edit my User ${sec.loggedInUserInfo(field: "id")}
</g:link>

使用firebug查看g:link…的准确呈现html

最新更新