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

user2918640
  • 473
  • 1
  • 7
  • 25

1 Answers1

5

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>
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • It worked. Thanks. Values in my property file are not alphabetically sorted but values in the html dropdown came up sorted alphabetically. Do you happen to know why? – user2918640 Jul 15 '14 at 07:41
  • let me test it at my end. – Braj Jul 15 '14 at 07:45
  • no it doesn't sort automatically. please can you share how are doing it? – Braj Jul 15 '14 at 07:48
  • I think either `propertyData` bean object internally keep it's keys sorted or `listVar` sorted them upon fetching during jstl. These are my properties: usa=USSAAA afghanistan=AFGHANISTAN-93 And dropdown shows in the order AFGHANISTAN-93 USSAAA – user2918640 Jul 15 '14 at 07:51
  • print the properties at server side then print it at client side without using option (select) component as I did in sample code. Let's see where it's getting sorted. – Braj Jul 15 '14 at 07:54
  • Well I want dropdown values alphabetically sorted. I tried alphabetical keys as well as numerical keys in their increasing order in file. The dropdown options are shuffled. Is it because of hashmap inability to maintain order? How do I keep my dropdown options in alphabetical order? None of these working `001=Athens 002=Bhutan` `Athens=Athens Bhutan=Bhutan` – user2918640 Jul 15 '14 at 10:37