1

I am trying to link to an image using a <c:url> from a jsp but it contains spaces in its file name. This means the url is converted to one with %20's in its name and doesn't find it. Is there anyway to achieve this without renaming every single file to not include spaces?

<ul id="abilityTabs" data-tabs="abilityTabs" class="nav nav-tabs">
    <li class="active"><a href="#passiveTab"><img id="passiveAbility" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability5}.png"/>" alt="Banner"></a></li>
    <li><a href="#ability1Tab"><img id="ability1" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability1}.png"/>" alt="Banner"></a></li>
    <li><a href="#ability2Tab"><img id="ability2" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability2}.png"/>" alt="Banner"></a></li>
    <li><a href="#ability3Tab"><img id="ability3" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability3}.png"/>" alt="Banner"></a></li>
    <li><a href="#ability4Tab"><img id="ability4" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability4}.png"/>" alt="Banner"></a></li>

Edit: I am an idiot, the files are jpg not png for some reason.

Le Ish Man
  • 451
  • 2
  • 4
  • 20
  • Posted, not sure what else would help – Le Ish Man Mar 26 '15 at 20:11
  • Check the double quotes. How are you able to put double quotes over and over again without getting an error?! This `src=""` should be something like `src=''`; otherwise the enclosed text between double quotes is not what you meant – x80486 Mar 26 '15 at 20:16
  • @Machina Although it does look strange, I wouldn't expect the nested double quotes to cause a problem in this case. The JSP parser doesn't parse the html, only the JSP. And the HTML parser in the browser would never see the JSP tags. So the 2 contexts won't interfere with each other. – Asaph Mar 26 '15 at 20:21
  • 1
    @Machina I can confirm that this isn't an issue although I will go ahead and change it just for good practice. My jsp is reading it almost correctly. – Le Ish Man Mar 26 '15 at 20:24
  • 1
    The `%20`s should work for filenames with spaces in them. I tested some files with spaces on a locally running tomcat. I suspect something else may be going on. Is `/resources` publicly browsable? – Asaph Mar 26 '15 at 20:44
  • @Asaph Yes, I am reading other images in a similar fashion on the same page – Le Ish Man Mar 26 '15 at 20:45

2 Answers2

0

Sounds like the problem was with the file extension and not encoding. But anyways...

Turns out <c:url> does not do URL encoding at all (except query string params). See How to encode a String representing URL path with JSTL?

You can use Spring's URL tag to encode template vars:

<spring:url value="/url/path/{variableName}">
   <spring:param name="variableName" value="more than JSTL c:url" />
</spring:url>
Community
  • 1
  • 1
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
0

If the string you are generating in the value is already exactly as you want it, why use c:url at all?

What is preventing you from changing this:

<img id="passiveAbility" src="<c:url value="/resources/images/abilities/${god.name} - ${god.ability5}.png"/>

to just this:

<img id="passiveAbility" src="/resources/images/abilities/${god.name} - ${god.ability5}.png"/>
alfreema
  • 1,308
  • 14
  • 26