0

I'm trying to access to a like this:

enter image description here

I pass to the JSP page

the list through request.setAttribute("list", list);

and try to access

<c:foreach items="${list}" var="element"}>

<li> ${element.name} ${element.price} </li>

</c:foreach>

but I get NumberFormatException. How can I access correctly the list?

eng_mazzy
  • 1,049
  • 4
  • 23
  • 39
  • Your `List` contains `Object[]` objects, not `Route` objects. How did you break type safety? – Sotirios Delimanolis Jun 25 '14 at 15:43
  • the question is quite more complex. I'm going to try to explain it better. I get the list through a JPA query where I get only two columns of a table Route. So if I get only two columns, the list shouldn't be of route element but of what? – eng_mazzy Jun 25 '14 at 15:46
  • I think that to solve properly the problem I should follow this example http://stackoverflow.com/questions/17202334/jpa-entity-manager-select-many-columns-and-get-result-list-custom-objects – eng_mazzy Jun 25 '14 at 15:48
  • Well, yes, you'll need to setup your JPA query to return the proper entity. – Sotirios Delimanolis Jun 25 '14 at 15:48

2 Answers2

1

If you select only a few columns from a table, JPA will return an array of objects for each row returned. i.e. it will return a List<Object[]> object. If you want to get back a list of Route objects you can write a constructor in the Route class that takes two values(name and pric and set the values appropriately in the constructor. You can then use the constructor in the JPA query like below to get Route objects: select new yourpackage.Route(name, price) from Route

Priyesh
  • 2,041
  • 1
  • 17
  • 25
0

There are two issues in your JSTL:

<c:foreach items="${list}" var="element"}>
    ...
</c:foreach>
  1. Its c:forEach not c:foreach.
  2. There is one extra } in the end.

It should be like this:

<c:forEach items="${list}" var="element">
     ...
</c:forEach>

There are two option. Try any one as per need.

  1. If the list contains Object[] then use ${element[0]}
  2. If the list contains Route then use ${element['name']} or ${element.name} or ${element.getName()}. Make sure Route class contains name as instance variable with getter & setter methods.
Braj
  • 46,415
  • 5
  • 60
  • 76