0

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
QUEdini
  • 3
  • 1
  • 3
  • The functional requirement is not clear and the code is not really self-documenting and the example of the failed attempt doesn't help much in understanding the requirement. I'd suggest to edit your question to describe the functional requirement in more detail and/or add an example in pseudocode or normal Java code how exactly you would like to iterate over the map, using `for` loop and so on. This way it's easier to translate it into the JSTL approach. – BalusC Dec 13 '10 at 03:47
  • Sorry I forget, that everyone hasn't been looking at the same thing that i have for the past 2 days. I have updated my description, hopefully it sheds some light on what i'm trying to do – QUEdini Dec 13 '10 at 12:43

1 Answers1

1

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 a Map will give you a Map.Entry which in turn has getKey() and getValue() methods.

Here's a basic example:

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

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 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.

First the (simplified) Tile class:

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

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);

The 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>

(no need for jsp:useBean by the way)

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Well perhaps better illustrating my resulting map will help at least get the point across of what i'm trying to get . the resulting tree map looks like this
    {Key, value}
    {0, bean(tileName,tiledescrip,iagePath)} - {1, bean(tileName,tiledescrip,iagePath)} - {2, bean(tileName,tiledescrip,iagePath)}

    . 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
    – QUEdini Dec 13 '10 at 13:17
  • You need to access the bean property then, like `${entry.value.tileName}`. – BalusC Dec 13 '10 at 13:20
  • The problem i'm Still running into is that for some reason my forEach isn't even acknowledging the map. should i not be using this to get to pull the global component to the page? – QUEdini Dec 13 '10 at 13:43
  • See answer update. Hope this puts you in the right direction. I have the impression that you're overcomplicating things. – BalusC Dec 13 '10 at 13:51
  • You may be right about the over complication, this method seems to be doing the trick..thanks a lot BalusC – QUEdini Dec 13 '10 at 13:57
  • i'm getting oot cause of ServletException. javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "value" with value "${entry.value}": Unable to find a value for "value" in object of class "java.lang.String" using operator "." (null) but when i do a – QUEdini Dec 13 '10 at 14:31
  • oh and when I try to just output ${entry.value} it outputs just like text, it doesn't actually try to resolve the value inside of the – QUEdini Dec 13 '10 at 14:37
  • Is `${test}` a map? The exception suggests it's not. Use `` instead of `` – BalusC Dec 13 '10 at 14:39
  • ${test} was the map in this even changing the name to map, c:out still shows the values above, but i still get null for the entry.value.. is supposed to from <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> right? – QUEdini Dec 13 '10 at 15:09
  • That's the ancient JSTL 1.0 URI. You need `http://java.sun.com/jsp/jstl/core` (with the `/jsp` part). See also [this answer](http://stackoverflow.com/questions/2400038/enabling-javaserverpages-standard-tag-library-jstl-in-jsp). – BalusC Dec 13 '10 at 15:20
  • thanks for the answer what i wanted LK_PY_ALLOW is of type tree map it was hashtable so ... ` ${entry.value.allowAd} ` – shareef Jul 20 '13 at 09:06