嵌套的Spring EL不起作用(Nested Spring EL does not work)

编程入门 行业动态 更新时间:2024-10-26 05:26:17
嵌套的Spring EL不起作用(Nested Spring EL does not work)

我在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.

更多推荐

本文发布于:2023-07-29 15:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   不起作用   Spring   EL   Nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!