0

I am trying to figure out how I can check if a stored sql procedure I am running will return more than one row as a resultset. I have a forEach method which prints out the rows, but I cannot seem to be able to check later weather the resultset is one or more rows. I only need to know how to check if the result is MORE than one row. All help is appreciated. Here is a little snipplet of my jsp.

<tr>
<td>
<c:forEach var="row" items="${ aStoredProcedure }">  
<fmt:formatNumber value="${ row.totalPrice }"pattern="#,##0.00"/>
<c:out value="${row.currency }"/>
(<c:out value="${ row.id }"/>)
</c:forEach>
</td>
</tr>

The above code works well and I included it here in case there is a way to use it somehow to check if there are more than one row returned (I had to rename some variables for liability reasons) but now I need to check weather or not the result gives more than one row, and I need to do this in a different table row. So far I've tried:

<c:if test="${ aStoredProcedure >'1'}">
 <tr>
  <td>
   <%--do something here if it is bigger than 1--%>
  </td>
 </tr>
</c:if>

and

<c:if test="${ row >'1'}">
 <tr>
  <td>
   <%--do something here if it is bigger than 1--%>
  </td>
 </tr>
</c:if>

But these don't work at all.. only returns JasperExceptions. I found good solutions for how to check if a resultset has data or not, but nothing for how to do it entirely in JSP and preferably without using java. As you can probably tell, I am very new to JSP and will appreciate any help on how to figure out how to check if the procedure returns more than one result.

I have a controller class as a java class, which houses the model that I use to put the model in place:

model.put("aStoredProcedure",managerClass.nameofMethodICallFromMngrClass(aValueIUseToCallMethod(),anotherValueIUseToCallMethod()) );

and for the time being I had to add a new part to the model to get the check done:

model.put("aStoredProcedureSize",managerClass.nameofMethodICallFromMngrClass(aValueIUseToCallMethod(),anotherValueIUseToCallMethod()).size() );

I then check the size to be bigger than 1 before doing anything on the JSP side:

<c:if test="${ aStoredProcedureSize gt 1}">
 <tr>
  <td>
   <%--do what I want here--%>
  </td>
 </tr>
</c:if>

This works well and does what I want, but I had to make changes to the java side and was wondering if the checking of the size could be done somehow entirely on the JSP side without having to make a new part to the model?

ghoulfolk
  • 326
  • 7
  • 17

2 Answers2

1

${ row >'1'} gives you malformed XML, therefore the JasperException. Try

${ row gt 1 }
Uooo
  • 6,204
  • 8
  • 36
  • 63
  • thanks for the advice, the gt seems to work well. However I noticed that I could not call the row from outside the original forEach which I determined it in, so I ended up creating a new piece of a model which I put in my controller class which is using regular java. So I ended up making a new "aStoredProcedureSize" model part in which is the exact same piece of model as "aStoredProcedure" with the exception that I call .size() in the end of it. I was wondering if the .size could have been called in the JSP side of the code? – ghoulfolk Jun 24 '13 at 10:13
  • 1
    @ghoulfolk that's a different question, but this might help you: http://stackoverflow.com/questions/851880/check-a-collection-size-with-jstl – Uooo Jun 24 '13 at 10:21
  • yeah that was what I was looking for, the .length method which I can call directly as " – ghoulfolk Jun 24 '13 at 11:45
1

You don't have to do '1', just use 1

so

<c:if test="${ row > 1 }">
 <tr>
  <td>
   <%--do something here if it is bigger than 1--%>
  </td>
 </tr>
</c:if>

should work provided row is the integer number of rows.

There are some nice examples for jstl operators here http://www.hscripts.com/tutorials/jsp/operators/relationalopr.php

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56