Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception grails异常:标记[paginate]缺少必需的属性[total]_Exception_Grails - Fatal编程技术网

Exception grails异常:标记[paginate]缺少必需的属性[total]

Exception grails异常:标记[paginate]缺少必需的属性[total],exception,grails,Exception,Grails,我的控制器中有以下方法,用于列出患者实体: def list(Integer max) { params.max = Math.min(max ?: 10, 100) def roles = springSecurityService.getPrincipal().getAuthorities() if(roles == 'ROLE_USER') { [patientInstanceList: Patient.findAllBySecUser(

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

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()]
    }
}
一切正常。我看到所有创建的患者实例。我只希望,根据登录用户的角色,我可以看到创建的所有实体的一部分。为什么会提出此异常


我在这里发现了一些类似的东西,但没有给出好的答案。我不确定您是否可以在if语句中使用返回。另一个细节是,您在
ROLE\u 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

}

该方法是自动生成的,没有任何return语句,但我将尝试您的代码…@FrancescoDS在Groovy中,您可以操作
return
。它将返回方法最后一行中的whet,因此在生成的代码中有一个隐藏的返回:-)