Different table rows

How do I use JSTL to set up a table.

In a scriptlet or a servlet, i can do something like

<td bgcolor="red">

<%

String results = rs.getString(1);

out.print(results);

%>

</td>

<td bgcolor="blue">

<%

results = rs.getString(2);

out.print(results);

%>

</td>

And so on.

With JSTL I've set it up a DB DAO and store the results in a List.

And then in my JSTL :

<tr>

<c:forEach items="${listResults}" var="row" varStatus="loop">

<c:forEach items="${row}" var="cell" ">

<td>${cell}</td>

</c:forEach>

</c:forEach>

</tr>

I get all the data results but I need to store certain things, certain ways.

Certain cells might be red, blue and so on.

I've tried to use ${cell[0]}or ${cell.1} or ${cell.value[1]} or ${cell["1"]} and so on but the only results I get are a blank html page.

I've also tried to store my list in a bean where I'd use cell.fname or cell.lname but again, the only results i get are a blank html page.

When I store a list of Maps, and then use the ${cell} i get all the results in the table, but i can't parse through them or change specific cells and so on.

Any help would be appreciated.

[1379 byte] By [pjamWeb76a] at [2007-11-27 2:23:50]
# 1

What is the datastructure you have? A List of Lists?

Can you show how you produced the attribute "listResults"

and also what is the type of "row"

Right now you have a nested loop.

You could just have one loop, and try accessing values

row[0], row[1], row[2] which would be closer to how you did the original scriptlet code.

Also, I'm changing your code a little, assuming one row in the table for each item in the "listResults" which means the <tr> tags should be inside that loop. Impossible to tell if that is your intention or not.

<c:forEach items="${listResults}" var="row" varStatus="loop">

<tr>

<td bgcolor="red">${row[0]}</td>

<td bgcolor="blue">${row[1]}</td>

</tr>

</c:forEach>

evnafetsa at 2007-7-12 2:29:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Here is some of the code :

The Servlet :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

try

{

HttpSession session = request.getSession(false);

if(session != null)

{

aSqlBean = (cims.DB.sql_Bean)session.getAttribute("the_Sql");

}

if(aSqlBean == null)

{

//servlet didn't get a handle to the bean object

//either the session is new or the jsp that created the bean

// hasntbeen executed yet

}

String theSite = aSqlBean.getSite();

String theReport = aSqlBean.getReport();

String theQuery = aSqlBean.getSql();

ArrayList theResults = siteRequest.getSite(theSite, theReport, theQuery, title);

session.setAttribute("listResults", theResults);

String reportFile = aSqlBean.getReport_file();

RequestDispatcher rd = getServletContext().getRequestDispatcher(reportFile);

rd.forward(request,response);

}

catch(Exception e)

{

System.out.print("Exception in doPost " + e.getMessage());

}

}

Here is where I read and store the Data :

public ResultSet getResultSet(String query, boolean close)

{

try

{

pstmt = dbCon.prepareStatement(query);

rs = pstmt.executeQuery();

}

catch(SQLException SQLe)

{

SQLe.printStackTrace();

}

finally

{

if(close)

{

closeObjects();

}

}

return (rs == null) ? null : rs;

}

Here is what I do with the ResultSet :

public ArrayList getData(String theQuery)

{

List resultsList = new ArrayList();

try

{

ResultSet rs = aDBBean.getResultSet(theQuery, false);

while (rs.next())

{

aResultBean = new abc.Beans.General.resultsBean();

aResultBean.setBms(rs.getString(1));

aResultBean.setMcc(rs.getString(2));

resultsList.add(aResultBean);

}

}

catch(SQLException sqle)

{

System.err.print("SQL ERROR " + sqle);

}

return (ArrayList) ((resultsList == null) ? null : resultsList);

}

I've played around with the above code.

However the only time I get any results are when I store the results in in a HashMap, sort them using a TreeMap, and then add the sorted HashMap to the ArrayList.

The way the code works above as posted, I get a blank html page for the results.

And here is my JSP page with JSTL :

<c:forEach items="${listResults}" var="row" varStatus="loop">

<tr>

<c:forEach items="${row}" var="cell">

<td>${row.bms}</td>

<td>${row.mcc}</td>

</c:forEach>

</tr>

</c:forEach>

I've also tried it as one c:forEach and so on. The only time I get results are when I use the HashMap.

Message was edited by:

pjamWeb76

pjamWeb76a at 2007-7-12 2:29:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I sort of got it to work if I store the results in a HashMap, sort it in a TreeMap the way I want it sorted, add the map to a List, and then display it in JSP using JSTL like this :

<c:forEach items="${row}" var="cell">

<c:if test="${cell.key==1}">

<td align="left" bgcolor="pink">${cell.value}</td>

</c:if>

<c:if test="${cell.key==2}">

<td align="left" bgcolor="blue">${cell.value}</td>

</c:if>

<c:if test="${cell.key==3}">

<td align="left" bgcolor="orange">${cell.value}</td>

</c:if>

<c:if test="${cell.key==4}">

<td align="left" bgcolor="red">${cell.value}</td>

</c:if>

</c:forEach>

Not sure if this is the best way, but this way I can change certain colors and so on and display the results the way I need to.

pjamWeb76a at 2007-7-12 2:29:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Prepopulate the colors in an array or List and retrieve the values with (cell.key - 1) as index. Then you don't need to write c:if blocks.
BalusCa at 2007-7-12 2:29:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...