如何将Spring属性迭代为JSP列表 [英] How to iterate spring properties as a jsp list

查看:72
本文介绍了如何将Spring属性迭代为JSP列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<bean id="propertyData"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean"
        lazy-init="false">
        <property name="mydata">        
                <value>classpath:external-data.properties</value>
        </property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="exposedContextBeanNames">
        <list><value>propertyData</value></list>
    </property>
</bean>

使用上面的代码片段,我能够加载属性文件并在jsp中对其进行访问.并在jsp中使用以下语法成功通过键访问值

Using the snippet above , I am able to load property file and access the same in jsp. And successfully accessing value by key using the following syntax in jsp

<option value='${propertyData.usa}'>${propertyData.usa}</option>

现在,我需要使用jstl或其他方式遍历jsp中的所有属性键,以使用这些键的值填充html下拉列表.

Now I need to iterate over all the property keys in jsp using jstl or other means to populate an html dropdown with the values of those keys.

以下内容不适用于我.

<core:forEach var="listVar" items="${propertyData}">
     <option value ="10"><core:out value="${listVar.attribute}"/></option>
</core:forEach>

错误是:

javax.el.PropertyNotFoundException: Property 'attribute' not found on type java.util.Hashtable$Entry
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)

请更正我要去哪里.

推荐答案

只需使用keyvalue对其进行访问.就像在JSP中迭代HashMap

Simply access it using key and value. It's just like iterating HashMap in JSP

如果您查看它是从java.util.Hashtable$Entry访问它的例外,因为属性扩展了Hashtable (后者又实现了Map<Object, Object>)

If you look at the exception that it's accessing it from java.util.Hashtable$Entry because Properties extends Hashtable (which, in turn, implements Map<Object, Object>)

Map.Entry 包含getKey()getValue()方法,用于从地图项"访问键和值.

Map.Entry contains getKey() and getValue() methods to access key and value from Map Entry.

示例代码:

<core:forEach var="listVar" items="${propertyData}">
    <core:out value="${listVar.key}"/>:<core:out value="${listVar.value}"/>
</core:forEach>

这篇关于如何将Spring属性迭代为JSP列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆