grails例外:标签[paginate]缺少必需属性[total] [英] grails exception: Tag [paginate] is missing required attribute [total]

查看:120
本文介绍了grails例外:标签[paginate]缺少必需属性[total]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've the following method in my controller, that is used to list the Patient entities:

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()]
    }
}

如果我执行此方法,我有异常

If I perform this method I have the exception

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()]
    }
}

一切顺利我看到患者创建的所有实例。我只想要,根据用户登录的角色,我可以看到创建的所有实体的一部分。为什么会出现这种异常?

Everything works well. I see all the instances of Patient created. I only want that, based on role of user logged in, I can see a part of all the entities created. Why does this exception is raised?

我在这里找到了 Grails分页标签错误类似的东西,但没有给出好的答案

I've found here Grails pagination tag error something similar, but no good answer was given

推荐答案

我不知道你可以在if语句中忽略return。另一个细节是您在 ROLE_USER 中过滤您的域记录,但统计独立于此过滤器的总数。

I'm not sure that you can ommit the return in a if statement. Another detail is that you're filtering your domain records in the ROLE_USER, but counting the total independent of this filter.

您可以尝试这种方法:

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

}

这篇关于grails例外:标签[paginate]缺少必需属性[total]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆