tags:

views:

222

answers:

5

The spring JDBC template returns me a list which looks like below:

[{CODE_C=CSC, DESC_C=CSC}, {CODE_C=PG1, DESC_C=PG1}] 

I want the above list to printed in jsp as below:

CSC-CSC
PG1-PG1

Can you tell me the best way to do this?

+1  A: 

That appears to be a List of Maps.

Assume you've assigned it to a List<Map<?,?>> named rows:

for (Map<?,?> row : rows)
  out.format("%s-%s ",row.get("CODE_C"),row.get("DESC_C"));
nicerobot
thanks all for the solutions.
Suresh S
A: 
List<String> results = new ArrayList<String>();

for (Map<String, String> map : someList) {
    StringBuilder sb = new StringBuilder();
    for (String s : map.values()) {
        sb.append(s).append('-');
    }
    String result = sb.toString();
    results.add(result.substring(0, result.length() - 1); // To cut the trailing '-'
}
// Do whatever with the results.
ponzao
@ponzao i want in java 1.4
Suresh S
I have to admit I've never programmed in Java 1.4 :) so some of the following advice might be totally wrong. Java 1.4 doesn't have StringBuilder, so you should use StringBuffer instead. Because generics were introduced in Java 1.5 you should drop the type information from the collections, for instance List<String> results = new ArrayList<String>() becomes List results = new ArrayList() and I guess you have to cast the items when adding them into the collection, so instead of results.add(result...) use results.add((String) result...). But as I previously mentioned I don't know much about 1.4.
ponzao
+1  A: 
<c:forEach var="map" items="${list_with_maps_inside}">
  ${map['CODE_C']} ${map['DESC_C']}
</c:forEach>
Vijay Prasad
This is the only right JSP answer. As the OP surprisingly didn't accept this answer it look like that he doesn't have JSTL installed and/or doesn't know how to do. You should have elaborated more about that instead of only posting a code snippet, Vijay :)
BalusC
+1  A: 

Although not really your question, I think it is best to use a Mapper to retrieve the results. If you do so, you will get a list of objects, which you can easily iterate using a for-loop.

Example from http://static.springsource.org/spring/docs/2.0.x/reference/jdbc.html:

public Collection findAllActors() {
    return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
}

private static final class ActorMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        Actor actor = new Actor();
        actor.setFirstName(rs.getString("first_name"));
        actor.setSurname(rs.getString("surname"));
        return actor;
    }
}
Fortega
A: 

${map['CODE_C']} ${map['DESC_C']}

is not working

Suresh S