2

These are my Controller and Command. Now i would like to get all fields names. Here is the link to get field names of domain class Get domain class field names. But i need to get field names of Command as i am explaining below

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;
}
}
Community
  • 1
  • 1
Ramesh
  • 416
  • 3
  • 10
  • This is almost certainly a bad idea. If you can describe what you are doing with the field names and what version of Grails you are using, there is probably a better way to do this than what is being suggested below. – Jeff Scott Brown Jun 10 '14 at 16:35
  • @JeffScottBrown why is this a bad idea? I was going to use something similar to loop through all the fields of a class and trim any string type variables. Are the methods below something that should be avoided? – Daniel Black Oct 05 '21 at 15:16
  • @DanielBlack "why is this a bad idea?" - There are a number of concerns. One is that doing runtime reflection like that during request processing is inefficient. There are better ways/times to gather that info. The question I posed above was to understand what is trying to be accomplished, and knowing that will point to better solutions. "Are the methods below something that should be avoided?" - Yes, some of that should be avoided if possible. – Jeff Scott Brown Oct 05 '21 at 15:34

2 Answers2

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

You can also try getting the properties from ExpandoMetaClass, as

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

in which case it.field ignores the class property from the metaClass.

Grails 2.2.0

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Hi @dmahapatro thanks for your reply, after executing your code ConnectionsAdvSearchCommand.declaredFields.each{ if(!it.synthetic) println "-->"+it.name } I got below output -->keyword -->fname -->lname -->title -->company -->school -->location -->country -->postalCode -->instanceControllerTagLibraryApi -->mimeTypesApi -->log -->instanceConvertersControllersApi Few extra fields are there in output please try to give a better solution – Ramesh Aug 30 '13 at 13:59
  • @user2729165 Which version of Grails do you use? – dmahapatro Aug 30 '13 at 15:17
  • -->title -->instanceControllerTagLibraryApi -->postalCode -->keyword -->location -->school -->lname -->companyName -->fname -->country -->metaClass @dmahapatro after executing your code i got above output. metaClass & instanceControllerTagLibraryApi are extra fields came in output. But i need exactly those fields is there any solution? – Ramesh Sep 02 '13 at 15:59
-1

Grails: Listing all fields/variables in a Domain/Model

In this example, I assume your domain class name is 'Research' and the full package name is 'com.package.name.Research'

Way 1: Research.declaredFields.each{

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

Way 2:

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

Way 3:

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

More info can be found at my blog

biniam
  • 8,099
  • 9
  • 49
  • 58