How do I call a Grails service from a gsp?

Best to use the tag library because creating a service instance directly in the view via the class loader WILL NOT autowire other services declared that may live in the service you are trying to use. Using the tag library you will have auto-wiring of those services. In your gsp view <g:customTag param1=”$modelObjec” param2=”someString” /> … Read more

grails-app/utils dir

the grails-app/utils/ directory is where items like Codecs go. I’m not sure what else goes there, but you can include other classes there and they will be included within the Application EDIT: The grails-app/utils/ directory is meant for “Grails specific utilities” (Artefacts, GrailsClasses, etc.) Jeff Brown(Core member of the Grails development team) responded to a … Read more

Grails – getting a message value from controller

Inside a controller or a taglib, you can use the following : g.message(code: ‘some.message’) However, inside domain classes or services, you need to inject messageSource and call getMessage() method from Sping class AbstractMessageSource. This snippet shows you how to do that: import org.springframework.context.i18n.LocaleContextHolder as LCH … class MyServiceOrMyDomain { def messageSource … messageSource.getMessage(code, msgArgs, defaultMsg, … Read more

Retrieving a list of GORM persistent properties for a domain

Try this: import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass … def d = new DefaultGrailsDomainClass(YourDomain.class) d.persistentProperties Here’s a link to the Grails API for GrailsDomainClass (it’s a link to an older version; I couldn’t find a newer one after some quick searches). It’s got a getPersistentProperties() (used in the code snippet above). You can traverse the API documentation to see … Read more

Detecting the platform (Window or Linux) by Groovy/Grails

System.properties[‘os.name’] will return the name of the OS, e.g. “Windows XP”. So if you want to figure out whether you’re running on Windows or not, you could do something like: if (System.properties[‘os.name’].toLowerCase().contains(‘windows’)) { println “it’s Windows” } else { println “it’s not Windows” } Alternatively, org.apache.commons.lang.SystemUtils (from the Apache commons-lang project) exposes some boolean constants … Read more

How does Grails know to apply a “layout” to pages it renders?

The <meta name=”layout” content=”main”> tag includes the layout in the gsp page. You can view the grails-app/views/layouts/main.gsp to view and modify the layout. You can copy main.gsp to mymain.gsp, modify it, then change layout entry in the gsp page to reference mymain.gsp instead of main.gsp and experiment with customizing your layout preserving your ability to … Read more

Grails controller rendering method render vs respond

The respond method uses content negotiation to respond with the most appropriate content type based on the requests ‘ACCEPT’ header. Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8, application/json This way the consumer of your site can choose how they wish to be returned data. This may not be the best option if you want to force a … Read more

How to access to a property defined in messages.properties file?

For reading property files in Groovy you can use the utility class ConfigSlurper and access the contained properties using GPath expressions. However, you have to be aware that ConfigSlurper doesn’t support standard Java property files. Normally the ConfigSlurper will be used to read .groovy files that may be similar to a property file, but adhere … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)