Struts2的遍历字符串数组(而不是字符串数组) [英] Struts2 iterating over a string array (not an array of strings)

查看:206
本文介绍了Struts2的遍历字符串数组(而不是字符串数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在官方文档Struts2的,我觉得code以下片段:

In the official Struts2 documentation I find the following snippet of code:

<s:iterator value="{1,2,3,4,5}" begin="2" end="4" >
   <!-- current iteration value (2,3,4) -->
   <s:property value="top" />
</s:iterator>

这迭代值1到5。从这个例子,我想有任何字符串 {} 括号将被视为数组。

This iterates over the values 1 to 5. From that example, I thought any string between {} brackets would be considered as an array.

我在Struts2的设置为包含类似于上面的一个字符串值的变量,但迭代总是把它看作1元件而不是元件的阵列。下面的工作实例无如预期。我用的%的不同组合{#}

I have a variable in Struts2 set to contain a string value similar to the one above, but the iterator always sees it as 1 element rather than an array of elements. None of the examples below work as intended. I used all different combinations of %{#}.

<s:set var="testa">{6,7,8,9,10}</s:set>
<s:iterator value="testa">
    <!-- <s:property/> -->
</s:iterator>

<s:set var="testb">{A,B,C,D,E}</s:set>
<s:iterator value="#testb">
    <!-- <s:property/> -->
</s:iterator>

<s:set var="testc">{F,G,H,I,J}</s:set>
<s:iterator value="%{#testc}">
    <!-- <s:property/> -->
</s:iterator>

<s:set var="testd">{K,L,M,N,O}</s:set>
<s:iterator value="%{testd}">
    <!-- <s:property/> -->
</s:iterator>

我所预期的那样的结果是:

what I expected as outcome was:

<!-- 6 -->
<!-- 7 -->
<!-- 8 -->
<!-- 9 -->
... and so on

但我真正得到的是:

But what I really got was:

<!-- {6,7,8,9,10} -->
<!-- {A,B,C,D,E} -->
... and so on

我在做什么错了?

what am I doing wrong?

请注意我不是在寻找各种方法来遍历Java对象的列表(我知道如何做到这一点),我真的要遍历数组的文本再presentation。

Please note I'm not looking for ways to iterate over a list of java objects (I know how to do this), I really want to iterate over a textual representation of an array.

推荐答案

用括号括起来的任意字符串保持字符串直到这个字符串被解析为OGNL前pression。在第一个例子的字符串包围的评价为OGNL前pression返回一个列表括号。这个清单被重复并打印结果。 Struts标签解析为OGNL前pressions 的在标签的属性,因此它不会对解析标签体。如果您需要在标签体来分析,您可以使用属性标记。因此,所有试图为列表失败对待一个字符串。在迭代器标签只接受Java对象的集合,并且不能重复一个字符串,因为一个字符串没有一个迭代器。

Any string enclosed in the brackets remains the string until this string is parsed for OGNL expression. In the first example the string enclosed with brackets evaluated as OGNL expression that returns a list. This list is iterated and results are printed. Struts tags parse for OGNL expressions only in tag's attributes, so it doesn't parse for tag body. if you need to parse in the tag body, you can use a property tag. So all attempts to treat a string as list failed. The iterator tag accepts only a collection of java objects and cannot iterate a string, because a string doesn't have an iterator.

我真的要遍历数组的文本再presentation

I really want to iterate over a textual representation of an array

所以,如果你转换的​​文本重新presentation 的一个数组,这是唯一可能的。在迭代器标签可以遍历数组。

So this is only possible if you convert a textual representation to an array. The iterator tag can iterate over arrays.

<s:set var="testa">{6,7,8,9,10}</s:set>
<s:iterator value="%{#testa.replaceAll('[\\\{\\\}]','').split(',')}">
    <!-- <s:property/> -->
</s:iterator>

这篇关于Struts2的遍历字符串数组(而不是字符串数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆