Rendering templates in services
Posted by admin - 28/11/09 at 01:11:30 pmIf you want to render gsp template on level of service (or in any other bean), just get org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib bean from spring context, and call method render on it:
class FooService { boolean transactional = false def grailsApplication def bar(view, model) { render(template:"/templates/emails/${view}", model:model) } private def render(args){ grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib').render(args) } }
Grails 1.1.2 released
Posted by admin - 27/11/09 at 12:11:53 amToday was released Grails 1.1.2 . There are some bug fixes and new functionalities (changelog). Rely on Robert Fletcher, version 1.2 will not appear, until Spring 3.0 goes final.
Pimp my Grails Bootstrap
Posted by admin - 11/05/09 at 10:05:14 amIf you want to depend data loaded in bootstrap from environment that application is currently running in, try this solution:
import grails.util.GrailsUtil import org.codehaus.groovy.grails.commons.GrailsApplication class BootStrap { def init = { servletContext -> if(!envMapping[GrailsUtil.environment]){ return } envMapping[GrailsUtil.environment]() } def destroy = { } def initTest = { ... } def initDev = { ... } def initProd = { ... } def envMapping = [(GrailsApplication.ENV_TEST):initTest, (GrailsApplication.ENV_DEVELOPMENT):initDev, (GrailsApplication.ENV_PRODUCTION):initProd] }
Variable envMapping is on the end BootStrap class becouse in other case values in map were set as null (???).
Grails upgrade from 1.0.4 to 1.1 problems – part 2
Posted by admin - 08/05/09 at 09:05:46 amIf after upgrade to version 1.1 you will be hit by error like this:
... log4j:ERROR Could not read configuration file from URL [file:/C:/GrailsApp/web-app/W java.io.FileNotFoundException: C:\GrailsApp\web-app\WEB-INF\classes\log4j.properties ...
go to web.xml template file in \src\templates\war\ directory of your project and delete lines:
... <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> ...
Grails file upload service
Posted by admin - 08/05/09 at 12:05:06 amIn ours application we want to create service that will move uploaded file to specified directory. We want only to give name for the file, relative path to the directory where file should be stored, and of corse a file. This is simple example of sutch service:
import org.springframework.context.* class FileUploadService implements ApplicationContextAware { ApplicationContext applicationContext boolean transactional = false def moveFile(file, folderRelativePath, fileName) { try { file.transferTo(new File(getAbsolutePath(folderRelativePath, fileName))) return true }catch(Exception exception){ log.error "File move error, ${exception}" } return false } private String getAbsolutePath(folderPath, fileName){ "${applicationContext.getResource(folderPath)).getFile()}${File.separatorChar}${fileName}" } }
We specify aboslute path for uploading file, to prevent problems with upload after application deploy on i.e. Tomcat server. To retrive absolute path we need application context, so we just implementing ApplicationContextAware interface.
Now we are ready to use service in ours controller:
... def logo= request.getFile('logo') if(!logo.empty) { if (fileUploadService.moveFile(logo, 'relative/path/to/dir', 'myLogo.jpg')){ render('file uploaded') }else{ render('error while file upload') } } ...
Grails upgrade from 1.0.4 to 1.1 problems – part 1
Posted by admin - 07/05/09 at 02:05:16 pmRecently I have been upgrading Grails application from version 1.0.4 to version 1.1. After that my test fails with error:
No signature of method: roles.beans() is applicable for argument types: (roles$_run_closure1) values: [roles$_run_closure1@b7ad1a]
I found that problem was with fixtures. In version 1.0.4 of Grails code like this:
beans { roleOne(Role) { name = ConfigurationHolder.config.security.role.user } }
works correctly, but after upgrade instant beans{} we should use fixture{} statment:
fixture { roleOne(Role) { name = ConfigurationHolder.config.security.role.user } }
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.