问题从的 TreeMap 中检索值在 JSP 中
一段时间以来,我在尝试从包含的 treeMap 中检索值时遇到了问题。我尝试了几种不同的访问数据的方法,但我所得到的最远的是 JSP 承认存在哈希或树图,但实际上并没有迭代它并返回数据。
我一直在尝试找到基于标签属性对 xml 结果进行基本排序的最佳方法。因此,我获取 xml 并将每个值放入一个 bean 中,然后获取并将这些 bean 放入树状图中,其中 Key=SortId。由于treema 自动排序,结果是XML 值的排序映射。 bean 和 treeMap 的数量工作得很好,问题只是将这些值拉出到 JSP 中
,也许更好地说明我生成的地图将至少有助于理解我想要得到的内容。目标是循环遍历新排序的列表,并用类似于
<div>
Name: ${tileName} <br />
Description: ${tileDescrip} <br />
<img scr="${imagePath}">
</div>
生成的树图之类的内容填充我的 divs 看起来像这样
sortedHash
{0, bean(tileName,tiledescrip,imagePath)}
{1, bean(tileName,tiledescrip,imagePath)}
{2, bean(tileName,tiledescrip,imagePath)}
我的问题是,使用上面的代码,我什至没有从第一级迭代中返回任何值地图,即使输出地图本身确实显示了它在那里,
sortedHash = CTTeamsiteXMLHash@135b24b
所以我正在做的伪流程
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
<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>
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.
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>
the resulting tree map looks like this
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.
Here's sample XML
<teaser>
<sort>1</sort>
<value1></value1>
<value2></value2>
</teaser>
My Component
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;
}
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管问题更新了,我仍然不明白你在视图方面到底需要什么。这一切都太模糊了。
无论如何,每次
c:forEach
对Map
的迭代都会给你一个Map.Entry
依次具有getKey()
和getValue() 方法。
这是一个基本示例:
这些知识应该可以帮助您入门。
另请参阅:
更新: I仍然不明白你用两个地图做什么,也不明白为什么你需要一个
Map
而不是List
,因为你不知道似乎对钥匙很感兴趣。因此,这里是一个仅包含一个映射和一个用于预处理请求的简单 servlet 类的示例。首先是(简化的)
Tile
类:预处理 servlet:
JSP:
(顺便说一下,不需要
jsp:useBean
)Despite of the question update, I still don't understand what exactly you need in the view side. It's all too vague.
Anyway, every
c:forEach
iteration over aMap
will give you aMap.Entry
which in turn hasgetKey()
andgetValue()
methods.Here's a basic example:
This knowledge should get you started.
See also:
Update: I still don't understand what you're doing with two maps and why exactly you need a
Map
instead of aList
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.First the (simplified)
Tile
class:The preprocessing servlet:
The JSP:
(no need for
jsp:useBean
by the way)