JSTL函数fn:split和fn:join不起作用



下面是不能正常工作的代码片段:

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%! String[] strings = {"happy","in 7th heaven","on cloud 8"}; %>
${fn:join(strings , '&')}
${fn:split("some/word/goes/here", "/")}

对我的问题的任何提示都是非常感激的,谢谢。

您正在尝试混合旧的scriptlet 与现代EL。这是行不通的。EL在页面、请求、会话和应用程序范围中搜索变量作为属性。它根本不搜索在(global) scriptlet作用域中声明的变量。

为了为EL准备变量,您需要将其设置为所需范围内的属性。通常,您会为此使用servlet或过滤器,或者可能使用请求/会话/上下文侦听器,但对于快速原型,您可能仍然希望使用老式的scriptlet。下面是一个将其置于请求作用域的示例:

<%
    String[] strings = { "happy", "in 7th heaven", "on cloud 8" };
    request.setAttribute("strings", strings);
%>

参见:

  • 我们的EL wiki页面
  • 如何避免Java代码在JSP文件?

最新更新