2

这些是我的控制器和命令。现在我想获取所有字段名称。这是获取域类字段名称的链接 获取域类字段名称。但是我需要获取 Command 的字段名称,如下所示

class SocialRecruitingController 
{
 def getFieldNames()
 {

 ConnectionsAdvSearchCommand.fields.each
 {it->
   log.info(it?.name+",");
// Expecting keyword,fname,lname,title,company,school,location,country,postalCode
 }


} 

class ConnectionsAdvSearchCommand
{
String keyword

String fname;
String lname;
String title;
String company;
String school;
String location;
String country;
String postalCode;
}
}
4

2 回答 2

4
ConnectionsAdvSearchCommand.declaredFields.each{
   if(!it.synthetic) println it.name
}

您也可以尝试从 ExpandoMetaClass 获取属性,如

ConnectionsAdvSearchCommand.metaClass.properties.each{
    if(it.field) println it.name
}

在这种情况下,it.field忽略metaClass.

圣杯 2.2.0

于 2013-08-29T15:16:39.510 回答
-1

Grails:列出域/模型中的所有字段/变量

在此示例中,我假设您的域类名称为“Research”,完整的包名称为“com.package.name.Research”

方式一:Research.declaredFields.each{

    if(!it.synthetic)
        println it.name
}

方式二:

def names = grailsApplication.getDomainClass('com.package.name.Research').persistentProperties.collect { 
    println it.name 
}

方式3:

def Research domainClass = grailsApplication.getGrailsDomainClass('Research')
def persistentProperties = domainClass.getPersistentProperties()
persisentProperties.each { property ->
    println property
}

更多信息可以在我的博客中找到

于 2014-06-10T16:03:08.560 回答