【问题标题】:Use of domain methods in Grails JSON View在 Grails JSON 视图中使用域方法
【发布时间】: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 这就是我的建议。是使用瞬变的更清洁的实现。然而,你是对的。我没有回答关于他编译错误的问题。

标签: grails json-view


【解决方案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 类中有一个名为 getFullName() 的方法,person.fullName 会起作用,但您没有。

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"}

【讨论】:

  • 这是正确的,查看您的示例项目会有所帮助。我发现的问题之一是,如果在我的域类中,如果我调用我的方法“def myMethod()”,它会给我一个 NoSuchMethodError,但如果我称它为“String myMethod”,它会正常工作。我原来是在做前者
  • 这对我来说没有意义,我无法重现它。方法的返回类型不应相关。
  • 您的评论与此问题中其他地方的一些项目相结合,向我表明您可能对属性与方法感到困惑。 per.fullName 将评估为 fullName 属性的值,这与调用 per.fullName() 将调用名为 fullName 的方法不同。
  • github.com/kingaaronm/fullnamequestion - 我将您的项目分叉为 domain/Person.groovy 以说明我的意思
  • 如果您在应用程序运行时更改域类中的方法签名,这将解释您所看到的行为。问题不在于您不能从 JSON 视图中调用域类上的方法。该问题与类重新加载问题有关。
猜你喜欢
  • 2021-11-19
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 2017-10-16
相关资源
最近更新 更多