Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c:url in c:set

Tags:

jsp

jstl

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'/>
like image 431
marioosh Avatar asked Dec 23 '11 09:12

marioosh


People also ask

What is the use of c url?

The <c:url> tag formats a URL into a string and stores it into a variable. This tag automatically performs URL rewriting when necessary. The var attribute specifies the variable that will contain the formatted URL. The JSTL url tag is just an alternative method of writing the call to the response.

What is the use of c set tag?

It is used to set the result of an expression evaluated in a 'scope'. The <c:set> tag is helpful because it evaluates the expression and use the result to set a value of java. util. Map or JavaBean.

Which of the following is the url for the core tag Spring framework?

JSTL <c:url> Core Tag. <c:url> JSTL tag is used for url formatting or you can say url encoding. This is mainly used when we need to open a JSP page based on the user input or based on the value of a variable. It basically converts a relative url into a application context's url.


2 Answers

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}"/>
like image 60
BalusC Avatar answered Nov 23 '22 17:11

BalusC


<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" />
like image 39
Bozho Avatar answered Nov 23 '22 18:11

Bozho