【发布时间】:2019-12-26 03:25:46
【问题描述】:
我已经使用 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
【问题讨论】:
-
“不编译”是什么意思?是否有错误或堆栈跟踪要分享?
-
@cfrick 问题已更新,日志文件中出现错误
-
您尝试过使用瞬变吗?看起来正是您正在寻找的:docs.grails.org/latest/ref/Domain%20Classes/transients.html
-
@JoshuaMoore 瞬态与编译错误无关。如果他在他的域类中有一个名为
getFullName()的方法,它们将是相关的,但这不是他/她所拥有的。我要做的是有一个getFullName()方法,使fullName瞬态,然后在视图中引用person.fullName,但是比他/她正在做的更好的原因,无助于解决编译问题所问的错误。 -
@JeffScottBrown 这就是我的建议。是使用瞬变的更清洁的实现。然而,你是对的。我没有回答关于他编译错误的问题。