1

I am able to get the list object but how do you display values in a list???

<%
  com.ibm.commerce.utf.objects.RFQProdAccessBean ab= new com.ibm.commerce.utf.objects.RFQProdAccessBean();
  Enumeration list = (java.util.Enumeration)ab.findByRFQId(java.lang.Long.parseLong("13001"));
  System.out.println("*******"+list);
  Vector aVector = new Vector(java.util.Arrays.asList(list));
  Iterator nums = aVector.iterator();

  while(nums.hasNext()) {
    String aString = (String)nums.next();
    System.out.println("************"+aString);
  }
%>    
<c:out value="${aString}" />
tim_yates
  • 167,322
  • 27
  • 342
  • 338
sandy444
  • 11
  • 2
  • 5
  • see the forEach examples in [this thread](http://stackoverflow.com/questions/2147495/accessing-java-based-dom-tree-directly-from-jsf-richfaces/2147716#2147716) – roemer Sep 14 '12 at 10:28

1 Answers1

1

You have several problems in your code:

  1. java.util.Arrays.asList(list) won't transform an Enumeration into a List. It will create a List containing a single element: the enumeration. You should use Collections.list() to do that.
  2. There is no reason to transform a List into a Vector. Vector (like Enumeration, by the way) is an old class that shouldn't be used anymore, and List has all you need.
  3. You should use generic types and not raw types. It would save youmany unnecessary casts
  4. This code shouldn't be in a JSP in the first place, but in a servlet or controller
  5. You're trying to access aString from outside the loop, but aString is defined inside the loop
  6. The JSP EL doesn't access local variables. It accesses page, request, session or application attributes. If you want it to access a local variable, you need to store this variable in a page scope attribute.
  7. I guess you want to display every string in the enumeration. In that case, you should use the c:forEach tag, and not a while loop.

So, the code could be written as such:

<% RFQProdAccessBean ab= new RFQProdAccessBean();
   Enumeration<String> enumeration = (Enumeration<String>) ab.findByRFQId(13001L);
   pageContext.setAttribute("list", Collections.list(enumeration));
%>
<c:forEach var="element" items="${list}">
    <c:out value="${element}"/>
</c:forEach>

But, I repeat, the Java code should be out of the JSP, and should store the list into a request attribute. The forEach loop would stay unchanged.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hi Nizet, thank you very much for your help.. but one more thing is in that ab.findByRFQId(13001L) is i am hard coding.. but i need to get that by dynamically, for that Enumeration enumeration = (Enumeration) ab.findByRFQId(request.getParameter(rfqBean.rfqId)); if i write like this i am getting "The method findByRFQId(Long) in the type RFQProdAccessBean is not applicable for the arguments (String) " – sandy444 Sep 14 '12 at 12:06
  • Then you indeed need a `Long.valueOf()` call (or similar). I simply took your code as is and simplified it as much as I could. – JB Nizet Sep 14 '12 at 13:01