从< id,beans>的TreeMap中检索值的问题在JSP中 [英] Issue Retrieving values from a TreeMap of <id, beans> in JSP

查看:61
本文介绍了从< id,beans>的TreeMap中检索值的问题在JSP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试从包含一段时间的treeMap中检索值时遇到问题.我尝试了几种不同的访问数据的方法,但是我得到的最充分的结果是让JSP承认有一个Hash或Tree Map,但实际上并没有遍历它并返回数据.

I have been have issues trying to retrieve the values from a treeMap that contains for some time. I have tried several different methods of accessing the data, but the furthest i have gotten was for the JSP to acknowledge there was a Hash or Tree Map, but not actually iterate through it and return data.

我一直在尝试寻找最佳方法,以根据tag属性对xml结果进行基本排序.因此,我要获取xml并将每个值放入一个bean中,然后获取,然后将这些bean放置在Key = SortId的树状图中.由于treema自动对结果进行排序,因此结果是XML值的排序图. bean和treeMap的填充效果很好,问题只是将这些值拉入了JSP

I have been trying to find the best way to basically sort xml results based on the tag attribute . So I am taking the xml and putting the values of each into a bean, then taking, then placing the beans in a treemap where Key=SortId. Since treema automatically sort the result is a sorted map of XML values. The population of the bean and treeMap works fine the issue is just pulling those values out in to the JSP

也许更好地说明我生成的地图至少可以帮助我理解要尝试的内容.目标是遍历新排序的列表,并用

Well perhaps better illustrating my resulting map will help at least get the point across of what i'm trying to get . The goal is to loop through the newly ordered list and populate my divs with something like

<div>
Name: ${tileName} <br />
Description: ${tileDescrip} <br />
<img scr="${imagePath}">
</div>

生成的树图如下所示

sortedHash

sortedHash

{0, bean(tileName,tiledescrip,imagePath)}
{1, bean(tileName,tiledescrip,imagePath)}
{2, bean(tileName,tiledescrip,imagePath)} 

我的问题是,使用上面的代码,即使输出地图本身确实在

My issue is that with the code above, I am not getting any values returned back from even the first level iteration of the map even though outputting the map itself does show its there at

sortedHash = CTTeamsiteXMLHash@135b24b 

所以我在做什么的伪流程

so the Pseudo flow of what i'm doing

Read XML
- Iterate
     -Parse XML Values to bean
  -Place bean in Treemap<SortId, XML-Bean>
-Return TreeMap
-Loop through treemap and then pull each bean value out. 

这里是示例XML

<teaser>
<sort>1</sort>
<value1></value1>
<value2></value2>
</teaser>

我的组件

public class CTTeamsiteXMLHash {


private HashMap<String, Object> xmlHash;
private TreeMap<String, Object> sortedHash;


public TreeMap<String, Object> getSortedHash() {
 return sortedHash;
}

public void setSortedHash(TreeMap<String, Object> sortedHash) {
 this.sortedHash = sortedHash;
}

public void setXmlHash(String sortOrder, CTTeamsiteXMLBean bean) {
 getXmlHash().put(sortOrder, bean);

}

public HashMap<String, Object> getXmlHash() {
 return xmlHash;
}

这是我尝试从JSP访问失败的尝试.如果有帮助,我也会在请求中传递sortedHash

Here is my failed Attempt at accessing from JSP. if it helps, i am passing the sortedHash in the request as well

     <jsp:useBean id="sortedHash" class="CTTeamsiteXMLHash"
 scope="request"/>
     <c:forEach items="${sortedHash.sortedHash}"
 var="eachItem">
       <c:forEach items="${eachItem.value}"
 var="anItem">
          <c:out value="${anItem.tileName.value}" /> :
 <c:out
 value="${anItem.tileDescrip.value}" />
       </c:forEach>
     </c:forEach>

推荐答案

尽管有问题更新,但我仍然不明白您在视图方面到底需要什么.都太模糊了.

Despite of the question update, I still don't understand what exactly you need in the view side. It's all too vague.

无论如何,在Map上的每个c:forEach迭代都会为您提供

Anyway, every c:forEach iteration over a Map will give you a Map.Entry which in turn has getKey() and getValue() methods.

这是一个基本示例:

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

这些知识应该可以帮助您入门.

This knowledge should get you started.

  • Iterating over arraylist inside hashmap in JSTL
  • Hidden features of JSP/Servlet

更新:我仍然不了解您正在使用两个映射的原因,以及为什么您以来确实需要Map而不是List似乎对按键不感兴趣.因此,这是一个只有一个映射和一个简单的Servlet类的示例,该类正在预处理请求.

Update: I still don't understand what you're doing with two maps and why exactly you need a Map instead of a List since you don't seem to be interested in the keys. So, here's an example with only one map and a simple servlet class which is preprocessing the request.

首先是(简化的)Tile类:

public class Tile {
    private String name;
    private String description;
    private string imagepath;
    // Add/generate c'tor/getters/setters/etc.
}

预处理servlet:

The preprocessing servlet:

Map<Integer, Tile> map = new TreeMap<Integer, Tile>();
map.put(1, new Tile("name1", "description1", "imagepath1"));
map.put(2, new Tile("name2", "description2", "imagepath2"));
map.put(3, new Tile("name3", "description3", "imagepath3"));
// ...

request.setAttribute("map", map); // It'll be available as ${map} in JSP.
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

JSP:

<c:forEach items="${map}" var="entry">
    <div>
        Map key: ${entry.key}<br>
        Tile name: ${entry.value.name}<br>
        Tile description: ${entry.value.description}<br>
        Tile image: <img src="${entry.value.imagepath}">
    </div>
</c:forEach>

(顺便不需要jsp:useBean)

这篇关于从&lt; id,beans&gt;的TreeMap中检索值的问题在JSP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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