2013-03-08 67 views
0

我在春天有一个类似下面的选择框。春季下拉框

<form:select path="cmbZone" multiple="false" class="validate[required] text-input tooltip" title="Mandatory select field."> 
    <form:option value="">Select</form:option> 

    <c:forEach items="${zoneList}" var="row"> 
     <form:option value="${row[0]}">${fn:escapeXml(row[1])}</form:option> 
    </c:forEach> 
</form:select> 

这使用JSTL forEach循环遍历项目列表。 zoneList是一个包含Object的数组的列表,如List<Object[]>,使用HQL从数据库中检索,如下所示。

List<Object[]>zoneList=sessionFactory.getCurrentSession().createQuery("select z.zoneId, z.zone from Zone z order by z.zoneId").list(); 

我想用<form:options>来实现相同的效果。我如何指定<form:options>itemLabelitemValue属性?

<form:select path="cmbZone" multiple="false" class="validate[required] text-input tooltip" title="Mandatory select field."> 
    <form:option value="">Select</form:option> 
    <form:options items="${zoneList}"/> 
</form:select> 

什么会在这个itemLabelitemValue

<form:options items="${zoneList}" itemLabel="" itemValue=""/> 

我使用的是Spring 3.2.0。我指的是this博客,但找不到方法。

回答

1

改变你的Hibernate查询到

List<Zone> zoneList = sessionFactory.getCurrentSession().createQuery(
    "select z from Zone z order by z.zoneId").list(); 

现在,你有属性的对象,您可以在此方面使用

<form:options items="${zoneList}" itemLabel="zone" itemValue="zoneId"/> 
+0

所以,我们没有选择的领域?数据库中的“区域”表中还有其他字段,包括'zone'和'zoneId',这些字段在这里不需要检索。难道只有选定的字段才能达到同样的效果吗? – Tiny 2013-03-08 23:01:45

+0

如果它适合在选择框中,加载所有字段与仅加载两个字段可能会产生微不足道的差异(如果有的话)。但是,您也可以创建一个LightZone对象,只有两个字段,并为从原始查询中检索的每一行创建一个LightZone实例。 – 2013-03-08 23:04:03

+0

这样做,但它真的不可能与'列表'?只是好奇。在JSF中,如果我可以正确记得,就有一个'var'属性。因此,这应该是可能的。 – Tiny 2013-03-08 23:19:44