2011-04-29 63 views
3

我想给登录的用户提供使用快速链接编辑他的用户帐户的可能性。变量不能在GSP标签中工作,但以普通文本工作

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

问题在于,当我处于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 Base Cla中的对象SS。

类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> 

回答

7

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

+0

非常感谢,这工作。有趣的是,IDEA不显示明显的错误... – 2011-05-01 20:22:58

+0

我向Grails报告了一大堆错误到IDEA - 它们快速修复闪电。 – 2011-05-02 02:34:37

0

你需要通过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的精确呈现的HTML:链接...

相关问题