1

GrailsDomainClass 类有两种方法:getPropertiesgetPersistentProperties

我有一个域类 ( MyDomainClass),其中包括:

static transients = {computeStuff}
float computeStuff(){
  def thisCouldChange = 3.1
  return thisCouldChange  //it actually does things but not of consequence to my question
}

好的,我采用了默认索引页面并对其进行了修改以列出所有属性MyDomainClass

<g:each var="d" in="${grailsApplication.domainClasses.sort { it.fullName } }">
<h2>${d.fullName}</h2>
<g:each var="f" in="${d.properties.sort { it.fieldName } }">
<br>${f.fieldName }
</g:each>
</g:each>

行。这行得通,但它没有得到任何瞬态属性。我已经尝试过 d.properties 和 d.persistantProperties ,它们似乎给了我相同的结果。在此先感谢您的帮助!!

我需要称它为 getComputeStuff 吗?

我现在已经更改了我的域类以包含它,但仍然没有取回瞬态 computeStuff

static transients = ['computeStuff']
float getComputeStuff(){
  def thisCouldChange = 3.1
  return thisCouldChange  //it actually does things but not of consequence to my question
}

这似乎没有任何区别。

4

2 回答 2

2

去除静态瞬态减速。如下定义您的方法:

def getComputeStuff(){
  def thisCouldChange = 3.1
  return 3.1  //it actually does things but not of consequence to my question
}

之后,属性调用“computeStuff”应该出现在您的属性列表中,调用域类上的 getProperties()。将返回值定义为def非常重要。

于 2011-06-17T06:39:20.047 回答
0

我认为,如果您希望将computeStuff方法视为属性,请将其getComputeStuff命名为 JavaBeans 的名称约定。我不确定它是否会起作用,但我认为值得一试;)

于 2011-06-16T23:33:30.060 回答