6

I need to use some link as argument to <spring:message /> and use <c:set/> for that. To have link relative to contextPath i use <c:url>. Why using <c:url/> in <c:set/> inside like below doesn't work ?

<c:set value='<c:url value="/x"/>' var='doc1'/>
<spring:message code="doc.link" arguments="${doc1}"/> <%-- ${doc1} is empty --%>

Simlar using <a href/> works good:

<c:set value='<a href="/fullurl/x">here</a>' var='doc1'/>
<spring:message code="doc.link" arguments="${doc1}"/>

messages.properties:

doc.link = Doc is {0}

EDIT I need to work exactly something like that:

<c:set value='<a href="<c:url value="/x"/>">here</a>' var='doc1'/>
marioosh
  • 27,328
  • 49
  • 143
  • 192

3 Answers3

14

Put it in the tag body:

<c:set var="doc1"><a href="<c:url value="/x" />">here</a></c:set>
<spring:message code="doc.link" arguments="${doc1}"/>

Or if you want XML well-formness:

<c:url var="url" value="/x" />
<c:set var="doc1"><a href="${url}">here</a></c:set>
<spring:message code="doc.link" arguments="${doc1}"/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
5

<c:url> has an option to set the result to a variable, rather than outputting it. Just set the var attribute.

<c:url value="..." var="doc1" />
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I don't agree. I'm using something like that: `"/>` without var attribute. – marioosh Dec 23 '11 at 09:19
  • 1
    don't agree with what? It has an _option_ to do that. If you don't use that option, it will output the result to the page. If you use it (specify `var`), it will be stored there instead – Bozho Dec 23 '11 at 09:26
4

You can do this:

<c:url var="myURL" value="/x" />
<spring:message code="doc.link" arguments="${myURL}" />

Because your message is doc.link = Doc is {0} where in the {0} appears at the end of the message, you can simply change the message to doc.link = Doc is and do as follows:

<spring:message code="doc.link" /><a href="<c:url value="/x"/>">here</a>

That will do exactly what you want to do!

adarshr
  • 61,315
  • 23
  • 138
  • 167