Json Views template not found when run as a war file

泪湿孤枕 提交于 2019-12-08 13:48:13

问题


I am using Json views plugin of grails. Which works in development, but when I run it as a jar file, it is not able to find the templates/gson files for rendering. I get the following error

//code 
    def template = jsonViewTemplateEngine.resolveTemplate(<path to template>)
            def writable = template.make(kase: kase)

//exception
    Cannot invoke method make() on null object. Stacktrace follows:
java.lang.NullPointerException: Cannot invoke method make() on null object

Json Views we are using are a part of a inline plugin we are developing. Jar we create also runs with that inline plugin (implemented using gradle wrapper)

Any ideas/suggestions?

Environment: Grails - 3.2.0 Groovy - 2.4.7 Json-Views plugin - 1.1.1


回答1:


Views are pre-compiled into classes for deployment via Gradle and the compileGsonViews task. This is done using the project.name setting by default.

You will notice in the build/main/gson-classes directory the classes that are produced. For example if your application name is foo you will have classes like foo_book_show_gson.class where the foo_ part is considered the "package" name.

At runtime. The package name to use to resolve views calculated from the info.app.name setting in grails-app/conf/application.yml.

What this means is that if in Gradle your project.name evaluates to foo and the setting in application.yml is also foo then all is well. This is the most common case as typically your application name is the same in both places.

If info.app.name and the Gradle project.name don't match up you can get the problem where views don't resolve.

You have two options to fix this. One is to modify build.gradle to explicitly specify the package name:

 compileGsonViews.packageName = 'foo'

Then make sure info.app.name matches that value.

The second option is rename your project directory so that info.app.name and project.name align.




回答2:


Json View behavior is different in dev mode and war mode.

WritableScriptTemplate template
if (Environment.isDevelopmentEnvironmentAvailable()) {
    template = attemptResolvePath(path)
    if (template == null) {
        template = attemptResolveClass(path)
    }
} else {
    template = attemptResolveClass(path)
    if (template == null) {
        template = attemptResolvePath(path)
    }
}
if (template == null) {
    template = NULL_ENTRY
}

In development mode grails will load your template by file path first then load by class name.
If you are working on case insensitive file system,template file like 'your_template_name' and 'YOUR_TEMPLATE_NAME' will be treated same.
But class name is case sensitive,'your_template_name_class' and 'YOUR_TEMPLATE_NAME_CLASS' is different;

Maybe your template file name is incorrect.

For example, if your classname is:

QueryResult

Your template file should located in:

/views/queryResult/_queryResult.gson

Json View will search class like:

<Project-Name>_queryResult__queryResult_gson


来源:https://stackoverflow.com/questions/40708115/json-views-template-not-found-when-run-as-a-war-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!