Rendering templates in services

If 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:

  1.  
  2. class FooService {
  3.  
  4. boolean transactional = false
  5.  
  6. def grailsApplication
  7.  
  8. def bar(view, model) {
  9. render(template:"/templates/emails/${view}", model:model)
  10. }
  11.  
  12. private def render(args){
  13. grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib').render(args)
  14. }
  15. }
  16.  

Grails 1.1.2 released

Today 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

If you want to depend data loaded in bootstrap from environment that application is currently running in, try this solution:

  1.  
  2. import grails.util.GrailsUtil
  3. import org.codehaus.groovy.grails.commons.GrailsApplication
  4.  
  5. class BootStrap {
  6.  
  7. def init = { servletContext ->
  8. if(!envMapping[GrailsUtil.environment]){
  9. }
  10.  
  11. envMapping[GrailsUtil.environment]()
  12. }
  13.  
  14. def destroy = {
  15. }
  16.  
  17. def initTest = {
  18. ...
  19. }
  20.  
  21. def initDev = {
  22. ...
  23. }
  24.  
  25. def initProd = {
  26. ...
  27. }
  28.  
  29. def envMapping = [(GrailsApplication.ENV_TEST):initTest,
  30. (GrailsApplication.ENV_DEVELOPMENT):initDev,
  31. (GrailsApplication.ENV_PRODUCTION):initProd]
  32. }
  33.  

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

If after upgrade to version 1.1 you will be hit by error like this:

  1.  
  2. ...
  3. log4j:ERROR Could not read configuration file from URL [file:/C:/GrailsApp/web-app/W
  4. java.io.FileNotFoundException: C:\GrailsApp\web-app\WEB-INF\classes\log4j.properties
  5. ...
  6.  

go to web.xml template file in \src\templates\war\ directory of your project and delete lines:

  1.  
  2. ...
  3. <context-param>
  4. <param-name>log4jConfigLocation</param-name>
  5. <param-value>/WEB-INF/classes/log4j.properties</param-value>
  6. </context-param>
  7. ...
  8.  

Grails file upload service

In 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:

  1.  
  2. import org.springframework.context.*
  3.  
  4. class FileUploadService implements ApplicationContextAware {
  5.  
  6. ApplicationContext applicationContext
  7.  
  8. boolean transactional = false
  9.  
  10. def moveFile(file, folderRelativePath, fileName) {
  11. try {
  12. file.transferTo(new File(getAbsolutePath(folderRelativePath, fileName)))
  13. }catch(Exception exception){
  14. log.error "File move error, ${exception}"
  15. }
  16. }
  17.  
  18. private String getAbsolutePath(folderPath, fileName){
  19. "${applicationContext.getResource(folderPath)).getFile()}${File.separatorChar}${fileName}"
  20. }
  21. }
  22.  
  23.  

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:

  1.  
  2. ...
  3.  
  4. def logo= request.getFile('logo')
  5.  
  6. if(!logo.empty) {
  7. if (fileUploadService.moveFile(logo, 'relative/path/to/dir', 'myLogo.jpg')){
  8. render('file uploaded')
  9. }else{
  10. render('error while file upload')
  11. }
  12. }
  13.  
  14. ...
  15.  

Grails upgrade from 1.0.4 to 1.1 problems – part 1

Recently I have been upgrading Grails application from version 1.0.4 to version 1.1. After that my test fails with error:

  1.  
  2. No signature of method: roles.beans() is applicable for argument types: (roles$_run_closure1) values: [roles$_run_closure1@b7ad1a]
  3.  

I found that problem was with fixtures. In version 1.0.4 of Grails code like this:

  1.  
  2. beans
  3. {
  4. roleOne(Role)
  5. {
  6. name = ConfigurationHolder.config.security.role.user
  7. }
  8. }
  9.  

works correctly, but after upgrade instant beans{} we should use fixture{} statment:

  1.  
  2. fixture
  3. {
  4. roleOne(Role)
  5. {
  6. name = ConfigurationHolder.config.security.role.user
  7. }
  8. }
  9.  

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.