Search code examples
grailsresourcesimagegsp

Grails 2.0: Use variable for image resource


I am passing a variable logo which contains the file name of an image file from my controller to the GSP and then I try to display the image like this:

<img src="${resource(dir:'images',file:"${logo}")}" alt="Logo" border="0" />

Even though the variable logo contains the correct value I get an Unclosed GSP expression error:

java.lang.RuntimeException: Error initializing GroovyPageView
at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:26) ~[plugin-classes/:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_26]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_26]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_26]
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Unclosed GSP expression
... 4 common frames omitted

Replacing ${logo} with the file name works.

What am I doing wrong?

Thanks a lot

Jonas


Solution

  • You are trying to embeed Expresion Language inside Expresion Language.

    Replace:

     <img src="${resource(dir:'images',file:"${logo}")}" alt="Logo" border="0" /> 
    

    By

     <img src="${resource(dir:'images',file:logo)}" alt="Logo" border="0" />
    

    Inside EL you can refer to variables directly