0

我已经使用 VUE 配置文件创建了一个 Grails 4.0 应用程序,并且正在使用 JSON 视图(http://views.grails.org/latest/#_json_views)并且一切正常,但我还没有找到使用域方法的方法.gson 模板

一个完美的例子:

Person.groovy 域类

class Person {

    String firstName
    String lastName

    String fullName(){
        return "$firstName $lastName"
    }
}

个人控制器

class PersonController {

    def show(){
      respond Person.get(params.id)
    }

}

/views/person/_person.gson

model {
    Person person
}

json {
    lastName person.lastName
    firstName person.firstName
    //fullName person.fullName() -- this line doesn't compile
}

这是我正在尝试做的一个基本示例,但我无法编译这样的东西,而且我什至没有在文档中看到它是否可能。我还尝试在域类“getFullName()”中调用该方法,然后在 gson 文件中执行“fullName person.fullName”,但这也不起作用。

有没有办法在 .gson 文件中使用域类的方法?

更新:这是带有 getFullName() 的堆栈跟踪日志的示例

[Static type checking] - No such property: fullName for class: Person
 @ line 8, column 8.
       fullName person.fullName
          ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:311)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1102)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:645)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:623)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:600)
    at grails.views.ResolvableGroovyTemplateEngine$_createTemplate_closure2.doCall(ResolvableGroovyTemplateEngine.groovy:430)
    ... 71 common frames omitted

这是作为 fullName() 方法的一个示例

[Static type checking] - Cannot find matching method Person#fullName(). Please check if the declared type is correct and if the method exists.
 @ line 8, column 8.
       fullName person.fullName()
          ^

1 error
4

1 回答 1

1

您在那里显示的错误消息之一包括以下内容:

[Static type checking] - No such property: fullName for class: Person
 @ line 8, column 8.
       fullName person.fullName
          ^

1 error

看起来您指的是person.fullName而不是person.fullName(). person.fullName如果您在Person名为 的类中有一个方法getFullName(),但您没有。

请参阅https://github.com/jeffbrown/fullnamequestion上的项目。

https://github.com/jeffbrown/fullnamequestion/blob/81cb45f176f887edf90de783a976c48154c3f9bc/server/grails-app/views/person/_person.gson

import fullnamequestion.Person

model {
    Person person
}

json {
    lastName person.lastName
    firstName person.firstName
    fullName person.fullName()
}

这很好用:

~ $ git clone https://github.com/jeffbrown/fullnamequestion.git
Cloning into 'fullnamequestion'...
remote: Enumerating objects: 144, done.
remote: Counting objects: 100% (144/144), done.
remote: Compressing objects: 100% (120/120), done.
remote: Total 144 (delta 5), reused 144 (delta 5), pack-reused 0
Receiving objects: 100% (144/144), 188.53 KiB | 2.62 MiB/s, done.
Resolving deltas: 100% (5/5), done.
~ $ 
~ $ cd fullnamequestion/
~ $ ./gradlew server:bootRun

> Task :server:bootRun

Grails application running at http://localhost:8080 in environment: development
<==========---> 83% EXECUTING [18s]
> :server:bootRun

发送请求以呈现视图:

~ $ curl http://localhost:8080/person/1
{"lastName":"Lee","firstName":"Geddy","fullName":"Geddy Lee"}
于 2019-08-21T15:43:40.097 回答