我在bean id globalConstants中有一个名为var的静态全局String常量。 在我的jsp中,我可以通过$ {globalConstants.var}获取它的内容。 现在我的var有一个如下所示的URL:
<a href="<spring:url value="/fan/foo.html"/>">Foo</a>但是,标记不会被评估为正确的URL。 但相反,整个标签就在那里。 无论如何我的var变量中的spring标签在调用时会被评估吗? 具体来说,给定的URL应该是这样的
<a href="/abc/fan/foo.html">Foo</a>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>最满意答案
我假设你有类似的东西
public class Constants { private String var = "<a href=\"<spring:url value=\"/fan/foo.html\"/>\">Foo</a>"; public String getVar() { return var; } }接着
model.addAttribute("globalConstants", new Constants());和
${globalConstants.var}无论如何我的var变量中的spring标签在调用时得到了评估
不,这里只有一个组件:表达式语言解析器。 它将看到${..}并执行适当的逻辑来解析表达式并将String结果直接写入HttpServletResponse OutputStream 。
你根本无法做你所要求的,你不应该。 保持视图元素和Java代码分离(除非Java代码用于写入响应)。
如果您需要像全球持有者那样的东西
<a href="<spring:url value="/fan/foo.html"/>">Foo</a>将它放在自己的JSP中,并将其import到您需要的每个JSP中。
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.
更多推荐
发布评论