Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate spring properties as a jsp list

<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>

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>

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.

The Following is not working for me.

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

Error is:

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

Please correct where I am going wrong.

like image 657
user2918640 Avatar asked Dec 08 '25 20:12

user2918640


1 Answers

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

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 contains getKey() and getValue() methods to access key and value from Map Entry.

sample code:

<core:forEach var="listVar" items="${propertyData}">
    <core:out value="${listVar.key}"/>:<core:out value="${listVar.value}"/>
</core:forEach>
like image 89
Braj Avatar answered Dec 11 '25 11:12

Braj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!