JSP- getting array data into jsp from java
I have to display data fetched from DB to JSP. I am using spring MVC. I have stored data in List in java. Now i need to access this list in JSP and display it in tabular form.
Data needs to represented in tabular form from List and VO, column names and row names coming from List and corresponding values for row/column from object. e.g
Column1 Column2 Column3 Column4
Row1 A 开发者_如何学JAVA B C D
Row2 E F G H
How can i pass this List and Object in jsp from java.
Thanks
Process it on servlet and store it in 2D array of
String
orList<List<String>>
Set it as attribute to request, forward request to jsp.
on jsp use JSTL to represent data. using
<c:forEach>
You can add your data in session and access it in table.jsp
.
Once you get data on JSP you can easily render as you want using html + jsp combination.
Hope this helps.
List<List<?>> data = new ArrayList<List<?>>();
int col = -1;
List<?> row;
while(rs.next())
{
if (col != rs.getInt(1))
{
col = rs.getInt(1);
row = new ArrayList<?>();
data.add(row);
}
row.add(rs.getObject(3));
}
getRequest().setAttribute(data);
精彩评论