简体   繁体   English

JSP表达式语言中的数组文字?

[英]Array literals in JSP Expression Language?

I've been able to do Array literals in Thymeleaf's expression language before (using...OGNL?...I think...). 以前,我已经能够使用Thymeleaf的表达语言来处理Array文字(使用... OGNL?...我认为...)。 Now I am using Spring/JSP. 现在我正在使用Spring / JSP。 Do the JSP or Spring Expression Languages support Array Literals? JSP或Spring Expression Languages是否支持数组文字? I can't find any reference to them. 我找不到对它们的任何引用。

UPDATE: This is basically what I'm trying to do: 更新:这基本上是我想要做的:

<c:set var="myUrls" value="${new String[] {
    mk:getUrl(),
    mk:getOtherUrl(),
    mk:getDifferentUrl()
}}" />

You could hack it together in a scriptlet-less way with a little help of maps, <jsp:useBean> and EL 2.2. 您可以在地图, <jsp:useBean>和EL 2.2的帮助下,以无脚本的方式将其合并在一起。

<jsp:useBean id="map" class="java.util.LinkedHashMap" />
<c:set target="${map}" property="url" value="${mk.url}" />
<c:set target="${map}" property="otherUrl" value="${mk.otherUrl}" />
<c:set target="${map}" property="differentUrl" value="${mk.differentUrl}" />
<c:set var="array" value="${map.values().toArray()}" />

<c:forEach items="${array}" var="item">
    ${item}<br/>
</c:forEach>

If your environment doesn't support EL 2.2, just stick to map and access it via the map. 如果您的环境不支持EL 2.2,只需坚持使用地图并通过地图进行访问即可。

<c:forEach items="${map}" var="entry">
    ${entry.value}<br/>
</c:forEach>

Noted should be that your underlying problem is bigger. 注意应该是您的潜在问题更大。 You shouldn't be manipulating the model directly in the view. 您不应该直接在视图中操纵模型。 The controller is responsible for that. 控制器对此负责。 You should be altering the model in such way so that it's exactly what the view expects. 您应该以这种方式更改模型,以使其完全符合视图的期望。 Eg by adding a property, or by letting the controller do the job. 例如,通过添加属性或让控制器来完成这项工作。

You can use scriptlet to use vanilla Java, in JSP1.x with <% /* Java */ %> block, in JSP2 with something like: 您可以在JSP1.x中使用<% /* Java */ %>块,在JSP2中使用scriptlet来使用普通Java,在JSP2中使用类似以下内容:

<jsp:scriptlet><![CDATA[
pageContext.setAttribute("myArray", new String[] { "a", "b", "c" });
]]></jsp:scriptlet>

<!-- so then ... -->
${myArray} <!-- or ... -->
<c:forEach items="${myArray}" var="str">
  <p>${str}</p>
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM