如何遍历具有地图作为jstl中的字段的对象列表?

我有一个类 Spravochnik,它有一个字段 HashMap。如何使用 jstl 对其进行迭代?我想要一个jsp页面给我看表。Spravochnik 映射将列名映射到给定表的列对象(列数可能因表而异)。这是我在控制器中的方法。



 @RequestMapping(value = "/list")

  public String getSpravochniks(Model m, @RequestParam String tableName)

  {

    List<Spravochnik> spravList =

        spravochnikService.findAll(tableName);

    m.addAttribute("spravList", spravList);

    return "list";

  }


当我有如下所示的 jsp 时,它显示错误属性限定名称必须在元素内是唯一的(id 是超类 Spravochnik 继承的字段



  <c:forEach var="sprav" items="${spravList}">

            <tr>

                <td>

                    ${sprav.id}

                </td>

                <td>

                    ${sprav.map}

                </td>

            </tr>

        </c:forEach>


Spravochnik.class


public class Spravochnik extends BaseEntity {


    private HashMap<String, Object> map;


    public HashMap<String, Object> getMap() {

        return map;

    }


    public void setMap(HashMap<String, Object> map) {

        this.map = map;

    }  


}

也可能是 SpravochnikRowMapper


public class SpravochnikRowMapper implements RowMapper<Spravochnik> {

    @Override

    public Spravochnik mapRow(ResultSet rs, int arg1) throws SQLException {

        Spravochnik spr = new Spravochnik();

        ResultSetMetaData rsmeta = rs.getMetaData();

        HashMap<String, Object> map = new HashMap<>();

        int columnCount = rsmeta.getColumnCount();


        for(int column = 1; column <= columnCount; column++){

            map.put(rsmeta.getColumnName(column),rs.getObject(column));

        }


        return spr;

    }

}


隔江千里
浏览 63回答 1
1回答

达令说

我认为你也必须迭代 map (map类的字段Spravochnik)。也许是这样:&nbsp;<c:forEach var="sprav" items="${spravList}">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ${sprav.id}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <c:forEach items="${sprav.map}" var="entry">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key = ${entry.key}, value = ${entry.value}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </c:forEach>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </c:forEach>&nbsp; &nbsp; 我认为你也必须迭代 map (map类的字段Spravochnik)。也许是这样:&nbsp;<c:forEach var="sprav" items="${spravList}">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ${sprav.id}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <c:forEach items="${sprav.map}" var="entry">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key = ${entry.key}, value = ${entry.value}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </c:forEach>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </c:forEach>&nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java