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

标签 exception grails

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

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

如果我执行此方法,我会有异常(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()]
    }
}

一切正常。我看到了所有创建的Patient实例。我只希望基于登录用户的角色,可以看到所有创建的实体的一部分。为什么会引发此异常?

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

最佳答案

我不确定您是否可以省略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

}

关于exception - grails异常:标记[paginate]缺少必需的属性[total],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537352/

相关文章:

exception - Erlang 中的 function_clause 和 badarg 有什么区别?

perl - 在现代 Perl 中编写异常类的最佳实践

grails - 将列的求和值与另一列进行比较-条件不正确

exception - 如何记录部署在 tomcat 中的特定包的异常

C# 在没有 try-catch block 的情况下重新抛出异常

javascript - 从 CORS 请求中捕获异步异常

regex - 价格输入模式html5

angular - 是否可以有一个 body 渲染 Angular 4

spring - Grails安全插件:在哪里存储其他用户数据?

Grails 3 - 获取服务中的 Assets 路径