Jonas
Jonas

Reputation: 860

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

Upvotes: 2

Views: 1682

Answers (2)

Lari Hotari
Lari Hotari

Reputation: 5310

Ernest is right that you shouldn't use a GString in this case. The actual error is that values aren't correctly quoted. You could also do

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

(notice the single-quotes and double-quotes, they are properly closed)

Upvotes: 4

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

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

Upvotes: 7

Related Questions