gpt4 book ai didi

grails - BootStrap.groovy中的语法错误

转载 作者:行者123 更新时间:2023-12-02 14:03:25 26 4
gpt4 key购买 nike

我正在使用带有SpringSecurity插件的Grails。我运行了s2-quickstart,所以用户域类
被命名为“User.groovy”,角色域类被命名为“Role.groovy”。因此,映射类被命名为“UserRole.groovy”。
然后,我修改了BootStrap.groovy以创建一个示例用户,这导致了一个令人讨厌的语法错误“Groovy:意外 token :UserRole @第19行,第2列”。当调用“UserRole.create”时。

这是我的BootStrap.groovy文件:

import com.foo.Role
import com.foo.User
import com.foo.UserRole


class BootStrap {

def springSecurityService

def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

def user = new User(
username: "user",
password: springSecurityService.encodePassword("user"),
enabled: true
)


UserRole.create user, userSecRole // <--- This is where the error happens


def init = { servletContext ->
}
def destroy = {
}
}

最佳答案

您已将代码放入主类定义中。

该代码应位于init闭包内,即:

import com.foo.Role
import com.foo.User
import com.foo.UserRole

class BootStrap {

    def springSecurityService

    def init = { servletContext ->
    def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

    def user = new User(
        username: "user",
        password: springSecurityService.encodePassword("user"),
        enabled: true
        )

    UserRole.create user, userSecRole     // <--- This is where the error happens
    }
    def destroy = {
    }
}

关于grails - BootStrap.groovy中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com