从Grails GSP中的User-class springSecurityService读取未生成的属性

时间:2015-04-03 21:08:54

标签: grails spring-security

我正在Grails中编写更新用户数据表单。它必须在更新前的字段中显示旧的用户数据。这是此表格的一部分:

<g:form class="form-signin" controller="VisitorSpace">
    <label for="login" class="sr-only">Login</label>
    <g:textField id="login" class="form-control" name="login" placeholder="Login"
        value="${applicationContext.springSecurityService.principal.username}" required="" autofocus=""/>
    <label for="firstName" class="sr-only">Your name</label>
    <g:textField id="firstName" class="form-control" name="firstName" placeholder="Your name"
        value="${applicationContext.springSecurityService.principal.firstName}" required="" type="text"/>
...
</g:form>

这是我的域用户级的一部分:

class Person {

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    String firstName
    String lastName
    String email
    boolean isAdminCafee = false
    static transients = ['springSecurityService']

    static constraints = {
        username blank: false, unique: true
        firstName blank: false
        lastName blank: false
        password blank: false
        email blank: false, unique: true
    }
    ...
}

域类的生成的username-property正确,但是当我尝试请求ungenerated firstName-property时,我收到错误:

URI:/restorator/visitorSpace/editPrivateDataClass:groovy.lang.MissingPropertyExceptionMessage:
No such property: firstName for class: grails.plugin.springsecurity.userdetails.GrailsUser

1 个答案:

答案 0 :(得分:0)

解决。我通过使用参数渲染gsp解决了这个问题。 gsp-controller的渲染动作:

def editPrivateData(){
        def user = springSecurityService.currentUser
        render (view:'editPrivateData.gsp', model: [user: user])
    }

在gsp-form中使用:

<g:textField id="firstName" class="form-control" name="firstName" placeholder="Your name" var = "user" value="${user.firstName}" required="" type="text"/>