【问题标题】:grails exception: Tag [paginate] is missing required attribute [total]grails 异常:标签 [paginate] 缺少必需的属性 [total]
【发布时间】:2013-09-03 10:41:43
【问题描述】:

我的控制器中有以下方法,用于列出患者实体:

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)


    def roles = springSecurityService.getPrincipal().getAuthorities()
    if(roles == 'ROLE_USER')
    {
        [patientInstanceList: Patient.findAllBySecUser(springSecurityService.getCurrentUser()), patientInstanceTotal: Patient.count()]
    }
    else if(roles == 'ROLE_ADMIN' || roles == 'ROLE_MANAGER')
    {
        [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
    }
}

如果我执行此方法,我会遇到异常

Tag [paginate] is missing required attribute [total]

但是,如果我使用以下内容

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
    }
}

一切正常。我看到创建的所有 Patient 实例。我只想这样,根据登录用户的角色,我可以看到创建的所有实体的一部分。为什么会引发此异常?

我在这里找到了Grails pagination tag error 类似的东西,但没有给出好的答案

【问题讨论】:

    标签: exception grails


    【解决方案1】:

    我不确定您是否可以在 if 语句中省略返回。另一个细节是您正在过滤ROLE_USER 中的域记录,但计算总数与此过滤器无关。

    你可以试试这个方法:

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
    
        def roles = springSecurityService.getPrincipal().getAuthorities()
    
        def model
    
        //I think roles is a list so your equals will not work...
        def admin = roles.find { it.authority == 'ROLE_ADMIN' || it.authority == 'ROLE_MANAGER' }
        if(admin) {
           model = [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
        } else {
          //createCriteria().list(params) will paginate...  
          def patientInstanceList = Patient.createCriteria().list(params) {
            eq('secUser', springSecurityService.currentUser)
          }
          //totalCount will trigger a query without the limit, but applying your filter
          model = [patientInstanceTotal: patientInstanceList.totalCount, patientInstanceList: patientInstanceList]
        }
    
        //and finally we return the model
        //return model
        //just to show that the return is optional
    
        model
    
    }
    

    【讨论】:

    • 该方法是自动生成的,没有任何返回语句,但我会试试你的代码...
    • @FrancescoDS 在 Groovy 中,您可以省略 return。它将返回您在方法的最后一行中拥有的内容,因此生成的代码中有一个隐藏的返回:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多