【问题标题】:null pointer exception with Grails rendering pluginGrails 渲染插件的空指针异常
【发布时间】:2015-06-24 01:04:27
【问题描述】:

我在使用渲染插件时遇到了一些问题。它总是返回一个空指针异常。我看到了严重的类似问题,但我没有发现我错在哪里。

  • 我的模板代码:/views/appRetail/_report.gsp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-strict.dtd">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Welcome to Production !</title>
    </head>
    <html>
       <body>
        REPORT
       </body>
    </html>
    
  • 我的控制器代码:

    class AppRetailController {
    
      def pdfRenderingService
    
      def renderFormPDF() {
    
        def apps = App.findAll()
        new File("test.pdf").withOutputStream { outputStream ->
            pdfRenderingService.render(template: '/appRetail/report', model: [apps:apps], outputStream)
        }
      }
    }
    

这是堆栈跟踪:

2015-04-17 10:31:54,552 [http-bio-8080-exec-4] ERROR   errors.GrailsExceptionResolver  - NullPointerException occurred when   processing request: [POST] /toolprod/appRetail/renderFormPDF
Stacktrace follows:
Message: null
Line | Method
->> 1281 | getPublicDeclaredMethods in java.beans.Introspector
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1141 | getTargetMethodInfo      in     ''
|    416 | getBeanInfo . . . . . .  in     ''
|    163 | getBeanInfo              in     ''
|     31 | init . . . . . . . . . . in     grails.plugin.rendering.document.RenderEnvironment
|     68 | with                     in     ''
|     60 | with . . . . . . . . . . in     ''
|     65 | generateXhtml            in     grails.plugin.rendering.document.XhtmlDocumentService
|     35 | createDocument . . . . . in     ''
|     36 | render                   in grails.plugin.rendering.RenderingService
|    348 | doCall . . . . . . . . . in     toolprod.AppRetailController$_renderFormPDF_closure1
|    347 | renderFormPDF            in toolprod.AppRetailController
|    198 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                 in     grails.plugin.cache.web.filter.AbstractFilter
|     82 | doFilterInternal . . . . in com.linkedin.grails.profiler.ProfilerFilter
|   1145 | runWorker                in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run                      in java.lang.Thread

这是我使用的版本:

  • 插件版本:编译“:rendering:1.0.0”
  • Grails 版本:2.5.0

【问题讨论】:

    标签: grails grails-plugin


    【解决方案1】:

    从堆栈跟踪看来,withOutputStream 闭包引起了一些混乱,但它在测试应用程序中对我有用。尝试运行 grails cleangrails compile 并重新运行应用程序。如果还不能解决,请删除目标目录并运行 clean 并再次编译。

    如果仍然存在问题,有几种不同的方法可以生成 PDF。无需将OutputStream 传递给iText 来呈现PDF,您可以获取生成的byte[] 数组并自己编写,例如

    def renderFormPDF() {
        def apps = App.findAll()
        ByteArrayOutputStream baos = pdfRenderingService.render(template: '/appRetail/report', model: [apps: apps])
        new File('test.pdf').withOutputStream { it.write baos.toByteArray() }
    }
    

    def renderFormPDF() {
        def apps = App.findAll()
        ByteArrayOutputStream baos = pdfRenderingService.render(template: '/appRetail/report', model: [apps: apps])
        new File('test.pdf') << baos.toByteArray()
    }
    

    【讨论】:

    • 谢谢你的回答,但我总是有同样的错误。我也试过 renderPng : def file = new File("test.png") file.write("hello") renderPng(template: “/appRetail/report”,模型:[imageBytes:file.bytes])
    【解决方案2】:

    您需要在 buildConfig 文件中添加此依赖项:

    dependencies {
        runtime 'org.springframework:spring-test:4.1.6.RELEASE'
    }
    

    【讨论】:

    • 解决获取依赖项的错误:在 grailsCentral (repo.grails.org/grails/plugins) 中找不到工件 org.springframework:spring-test:zip:4.1.6.RELEASE (使用 --stacktrace 查看完整跟踪)
    • 仍然需要在 Grails 3.3.12 中渲染 2.0.3。原因是来自 grails-web-common 的 GrailsWebMockUtil 使用来自 spring-test 的 MockHttpServletRequest 而没有将其作为运行时依赖项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多