0

我在stackoverflow中搜索grailsApplication.controllerClasses过滤器,然后我遵循正确的答案。

我写这样的代码。

  <g:each var="c" in="${grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName } }">
      <li>
        <g:link controller="${c.logicalPropertyName}" action="list" data-pjax='#main'>
                   <g:message code="${c.logicalPropertyName}.label" default="${c.logicalPropertyName}"/>
        </g:link>
      </li>
 </g:each>

它导致错误堆栈

    org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error applying layout : bootstrap
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Error executing tag <g:include>: Unable to execute include: Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName }] on line [13]: null
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <g:include>: Unable to execute include: Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName }] on line [13]: null
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp$_run_closure2.doCall(bootstrap.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp.run(bootstrap.gsp:21)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Unable to execute include: Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName }] on line [13]: null
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp$_run_closure2.doCall(bootstrap.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp.run(bootstrap.gsp:21)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName }] on line [13]: null
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp$_run_closure2.doCall(bootstrap.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp.run(bootstrap.gsp:21)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName }] on line [13]: null
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_includes_subnav_gsp.run(subnav.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp$_run_closure2.doCall(bootstrap.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp.run(bootstrap.gsp:21)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)
root cause

java.lang.NullPointerException
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_includes_subnav_gsp$_run_closure3_closure8.doCall(subnav.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_includes_subnav_gsp$_run_closure3.doCall(subnav.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_includes_subnav_gsp.run(subnav.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp$_run_closure2.doCall(bootstrap.gsp:13)
    Users_wenhaoliu_Documents_workspace_posplus_grails_app_views_layouts_bootstrap_gsp.run(bootstrap.gsp:21)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)

但是如果我将代码更改为

<g:each var="c" in="${grailsApplication.controllerClasses.each{GrailsClassUtils.isClassBelowPackage(it.class, ['com.posplus.product']) }.sort{ it.fullName } }">
       <li>
            <g:link controller="${c.logicalPropertyName}" action="list" data-pjax='#main'>
                  <g:message code="${c.logicalPropertyName}.label" default="${c.logicalPropertyName}"/>
            </g:link>
      </li>
</g:each>

它列出了所有控制器。这里有什么问题?

4

2 回答 2

0

我处理那个。因为GrailsClassUtils.isClassBelowPackage不能处理非包类。我写了一个函数来解决这个问题。

public static boolean isClassBelowPackage(Class<?> theClass, List<?> packageList) {
    if(theClass.package==null)return false
    String classPackage = theClass.package.name;
    for (Object packageName : packageList) {
        if (packageName != null) {
            if (classPackage.startsWith(packageName.toString())) {
                return true;
            }
        }
    }
    return false;
}

在 gsp 中是:

<g:each var="c" in="${grailsApplication.controllerClasses.findAll{MyGrailsClassUtils.isClassBelowPackage(it.clazz, ['com.posplus.product']) }.sort{ it.fullName } }">
      <li>
        <g:link controller="${c.logicalPropertyName}" action="list" data-pjax='#main'>
                   <g:message code="${c.logicalPropertyName}.label" default="${c.logicalPropertyName}"/>
        </g:link>
      </li>
 </g:each>
于 2012-05-21T05:44:51.093 回答
0

虽然我确信你的答案工作得很好,但我不明白你为什么不使用原来的GrailsClassUtils. 您需要做的就是导入它们。

这就是我所做的,它工作得非常完美。

我无意使用答案进行回复,但由于缺乏声誉,我不得不这样做。

于 2015-01-22T14:33:49.163 回答