grails - 在 Grails JSON 视图中使用域方法
问题描述
我已经使用 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
解决方案
您在那里显示的错误消息之一包括以下内容:
[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上的项目。
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"}
推荐阅读
- c# - 如何在没有 DbContext 的 C# Dotnet Core 应用程序中创建 NpgsqlConnection
- swiftui - SheetView 中 SwiftUI .constant(nil) 绑定的意外行为
- python - 项目euler的ex 94的Python解决方案
- android - Android recyclerView 和数据绑定
- python - 如何创建特定的上三角矩阵?
- c# - EF 6 Code First 中的更新数据库错误
- apache - 代理 Apache2 之后的重定向
- python - 是否有像 Scala 那样的 Pythonic 隐含规定?即用于 FLOP 计数
- python-3.x - 对于这个问题,我可以得到一个概括的方法吗?
- python - 根据python中的字符列表生成字符串变体