gpt4 book ai didi

grails - 在Grails JSON View 中使用域方法

转载 作者:行者123 更新时间:2023-12-02 15:16:31 25 4
gpt4 key购买 nike

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

一个很好的例子:

Person.groovy域类

class Person {

String firstName
String lastName

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

人员 Controller
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()的stacktrace日志的示例
[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

最佳答案

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

[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

发送请求以渲染 View :
~ $ curl http://localhost:8080/person/1
{"lastName":"Lee","firstName":"Geddy","fullName":"Geddy Lee"}

关于grails - 在Grails JSON View 中使用域方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com