简体   繁体   English

如何使用JSTL遍历可能无限的集合的集合

[英]How iterate over a potentially infinite collection of collections using JSTL

Preface: I am a long-time fan of SO, read stuff here almost everyday -- but this is my first question -- thank you to everyone who has made SO such a fantastic resource! 前言:我一直是SO的忠实拥护者,几乎每天都在这里读东西-但这是我的第一个问题-感谢所有使SO成为如此出色资源的人!

Environment: Spring/JSTL/Java ; 环境:Spring / JSTL / Java; the constraints of this project are such that I cannot accept a solution which requires abandoning Spring/Java, though I could probably implement any standard view in place of JSTL. 该项目的局限性使得我无法接受需要放弃Spring / Java的解决方案,尽管我可能可以实现任何标准视图来代替JSTL。

Note: This question is not about how to iterate over a simple collection in JSTL; 注意:这个问题不是关于如何遍历JSTL中的简单集合的。 I know how to do that. 我知道该怎么做。 Additionally, I have searched SO and Google for an answer to this problem and have yet to find an answer. 此外,我已经在SO和Google上搜索了此问题的答案,但尚未找到答案。 I am willing to admit that maybe that indicates that I am approaching the problem incorrectly -- if so, I would appreciate an explanation of a better way to approach it. 我愿意承认,也许这表明我在错误地解决了这个问题-如果是这样,我将为您提供一个更好的解决方法的解释。


Problem: I have a data-service from which I obtain a collection of collections. 问题:我有一个数据服务,可以从中获得集合的集合。 I do not control this service, nor am I able to define it -- someone else is in charge of that, and it can change at any time. 我无法控制此服务,也无法定义它-其他人负责该服务,并且它可以随时更改。 Each item in the collection can be another collection (which I must parse), so the entire collection is theoretically unbounded. 集合中的每个项目都可以是另一个集合(我必须对其进行解析),因此整个集合在理论上是不受限制的。 The collection is basically like this: 该集合基本上是这样的:

  • Item name : String 件姓名:字符串
    • Item : Object (can be a String or another Collection) 项目:对象(可以是字符串或其他集合)
      (this sequence is potentially infinite) (此序列可能是无限的)

Essentially each item is a "service" that my client offers to their customers -- for example: "Configure your institutional-email", and each collection is a "category" of related services -- for example: "Email Services Offered". 本质上,每个项目都是我的客户向其客户提供的“服务”,例如:“配置您的机构电子邮件”,每个集合都是相关服务的“类别”,例如:“提供的电子邮件服务”。 But the category of "Email Services Offered" could actually be an item/object/collection of a larger item/object/collection that is more general -- for example: "Communication Services Offered". 但是“提供的电子邮件服务”类别实际上可以是更通用的较大的项目/对象/集合的项目/对象/集合-例如:“提供的通信服务”。

Current Solution: In my Spring Controller I currently get this collection as a Map and place it onto my Model. 当前解决方案:目前,在我的Spring Controller中,我将此集合作为Map获取,并将其放置到我的Model上。 In my View (JSTL) I (currently) iterate through 2-levels of the collection, where I assume (the one thing I can assume about this) that the first level is all going to be sub-collections (ie: 'categories'), and where I also assume (incorrectly) that the second level is all going to be items (ie: 'services'). 在我的View(JSTL)中,我(当前)遍历集合的2个级别,在此我假设(我可以对此做一件事),第一个级别将全部是子集合(即:“类别”) ),并且我还(错误地)假设第二层都是物品(即“服务”)。

<ul>    
  <c:forEach items="${serviceCategories} var="category">  // iterate through each collection

    <li>${category.key}  //the user-displayable title of the 'category' ('collection')

      <ul>
        <c:forEach items="${category.value}" var="service">  // iterate through each item
          <li>${service.key}</li>  //the user-displayable title of the 'service' ('item')
        </c:if>
      </ul>

    </li>

  </c:if>
</ul>

So I guess my question is that, given such a data-structure, how do I represent it in a view that can manage this??? 所以我想我的问题是,在给定这样的数据结构的情况下,我该如何在可以管理此数据的视图中表示它? I am bound by the client's desire to have this structure represented in a tree-like structure. 客户希望将这种结构表示为树状结构,这使我受其束缚。

If I could manipulate the view using pure-java then I would expect one way to achieve this would be to use recursion... something like this, I guess: 如果我可以使用纯Java操作视图,那么我希望实现这一目标的一种方法是使用递归...类似这样的东西,我猜:

/** NOTE:  I am not suggesting this is a great example of recursion, nor that it even
 *  would result in correct output; it was one-off'd just to provide something of an 
 *  example of how I imagine this could be done in Java if I had to do it this way.
 */
private StringBuilder buildString( Map<String, Object> dataStruct ) {

  StringBuilder view = new StringBuilder();

  Set<String> keys = dataStruct.getKeys();
  for( String key : keys ) {

    Object value = dataStruct.get( key );
    if( value instanceof String ) {

      view = key + "[" + value + "]";
      return;
    }

    String subView = buildString( value );
    view = key + "[" + subView + "]";
}

Gosh, I hope that is all clear-as-mud and not overwhelming. 天哪,我希望这一切都是泥泞而不会压倒一切。 I also hope I am simply overlooking (or ignorant of) a more elegant-solution that is readily recognized by someone who can help me out. 我也希望我只是忽略(或不了解)一个可以帮助我的人轻易认可的更优雅的解决方案。

Thank you for your time! 感谢您的时间! :D :D

Anytime you have hierarchical data that has unknown depth, and you want to display it, I think a good option is to turn it into XML and bind the XML to some type of tree display. 每当您拥有深度未知的层次结构数据并想要显示它时,我认为一个不错的选择就是将其转换为XML并将XML绑定到某种类型的树形显示中。 For example, in a similar situation in one of my applications (also Java/Spring) I use XStream to turn the collection into XML (you simply point XStream at the root object and it follows all of the paths for you), and then I display the results using the jQuery plugin jsTree . 例如,在类似的情况下,在我的一个应用程序(也是Java / Spring)中,我使用XStream将集合转换为XML(您只需将XStream指向根对象,它就会为您遵循所有路径),然后我使用jQuery插件jsTree显示结果。

If you don't want to use javascript, an alternate approach would be to use something like Jakob Jenkov's Tree Tag . 如果您不想使用javascript,另一种方法是使用Jakob Jenkov的Tree Tag之类的东西。

另一种选择是创建一个JSP简单标记( .tag文件),该标记以递归方式调用自身。

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

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