spring JDBC模板返回一个如下所示的列表:
[{CODE_C=CSC, DESC_C=CSC}, {CODE_C=PG1, DESC_C=PG1}]
我希望上面的列表在jsp中打印如下:
CSC-CSC
PG1-PG1
你能告诉我最好的方法吗?
答案 0 :(得分:2)
<c:forEach var="map" items="${list_with_maps_inside}">
${map['CODE_C']} ${map['DESC_C']}
</c:forEach>
答案 1 :(得分:1)
这似乎是地图列表。
假设您已将其分配给名为List<Map<?,?>>
的{{1}}:
rows
答案 2 :(得分:1)
虽然不是你的问题,但我认为最好使用Mapper来检索结果。如果这样做,您将获得一个对象列表,您可以使用for循环轻松迭代这些对象。
来自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;
}
}
答案 3 :(得分:0)
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.