0

I have a static global String constant named var within a bean id globalConstants. In my jsp, I can grab the content of it by ${globalConstants.var} . Now my var has a url like below:

<a href="<spring:url value="/fan/foo.html"/>">Foo</a>

However, the tag does not get evaluated to the correct url. But instead the whole tag is there. Is there anyway for my spring tags in the var variable get evaluated upon calling it ? Specifically, the given url should be something like this

 <a href="/abc/fan/foo.html">Foo</a>
user3480524
  • 35
  • 1
  • 6

2 Answers2

0

I believe you have forgot to import taglib like:

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

then below line should work:

 <a href='<spring:url value="/fan/foo.html"/>'>Foo</a>
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
  • its not like that. The anchor is part of a string variable. The content of the string variable is like this ${globalConstants.var} . However, the url:spring tag in the variable is not getting evaluated. Hence, I cant have a proper link – user3480524 Mar 31 '14 at 12:28
0

I'm assuming you have something like

public class Constants {
    private String var = "<a href=\"<spring:url value=\"/fan/foo.html\"/>\">Foo</a>";
    public String getVar() {
        return var;
    }
}

and then

model.addAttribute("globalConstants", new Constants());

and

${globalConstants.var}

Is there anyway for my spring tags in the var variable get evaluated upon calling it

No, there is only one component at play here: the Expression Language resolver. It will see ${..} and perform the appropriate logic to resolve the expression and write the String result directly to the HttpServletResponse OutputStream.

You simply cannot do what you're asking and you shouldn't. Keep your view elements and your Java code separate (unless the Java code is meant to write to the response).

If you need something like a global holder for

<a href="<spring:url value="/fan/foo.html"/>">Foo</a>

put it in its own JSP and import it in each JSP you need it.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724