根据上下文的Grails NPE?

时间:2014-11-19 15:31:58

标签: grails model-view-controller nullpointerexception gsp

My Grails(2.4.2)项目的基本结构:

my-app/
    grails-app/
        views/
            web/
                index.gsp
                signin.gsp
            admin/
            app/
        controllers/
            myapp/
                WebController.groovy
    <rest of project is a normal Grails app>

我的application.properties

#Grails Metadata file
#Thu Nov 06 14:21:10 EST 2014
app.grails.version=2.4.2
app.name=my-app
app.context=/
app.version=0.1

我的UrlMappings

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller: "web")
    }
}

我的WebController

package myapp

class WebController {
    def index() {
        render(view: "web/index")
    }

    def signin() {
        render(view: "web/signin")
    }
}

当我run-app时:

|Loading Grails 2.4.2
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
....................................
|Running Grails application
|Server running. Browse to http://localhost:8080/

点击该链接(http://localhost:8080/)时:

....[ERROR] 2014-11-19 10:22:44,029 org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /
Stacktrace follows:
java.lang.NullPointerException
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

我的意图如下:

  • WebController
  • 控制的所有网页
  • 存储在grails-app/views/web
  • 下的所有网页浏览量
  • 网址映射,http://localhost:8080/将您带到grails-app/views/web/index.gsp
  • 网址映射,http://localhost:8080/signin将您带到grails-app/views/web/signin.gsp

这里发生了什么?

2 个答案:

答案 0 :(得分:2)

您遇到的一个问题是:

// ...

render(view: "web/index")

// ...

render(view: "web/signin")

// ...

问题在于您指定了相对路径。由于您位于grails-app/views/web/控制器中,因此它将与Web相关。你可以像他们一样绝对......

// ...

render(view: "/web/index")

// ...

render(view: "/web/signin")

// ...

但更常见的事情是允许他们像这样相对......

// ...

render(view: "index")

// ...

render(view: "signin")

// ...

答案 1 :(得分:0)

我的问题是

render(view: 'myview')

但需要

render(template: 'myview')