【问题标题】:How to iterate spring properties as a jsp list如何将spring属性迭代为jsp列表
【发布时间】:2014-07-15 07:23:07
【问题描述】:
<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>

使用上面的 sn-p,我可以加载属性文件并在 jsp 中访问它。并在jsp中使用以下语法成功通过key访问值

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

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

以下内容不适合我。

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

请纠正我哪里出错了。

【问题讨论】:

    标签: spring jsp spring-mvc jstl


    【解决方案1】:

    只需使用keyvalue 访问它。就像iterating HashMap in JSP

    如果您查看它从java.util.Hashtable$Entry 访问它的异常,因为Properties extends Hashtable(反过来,实现Map&lt;Object, Object&gt;

    Map.Entry 包含 getKey()getValue() 方法来访问 Map Entry 中的键和值。

    示例代码:

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

    【讨论】:

    • 成功了。谢谢。我的属性文件中的值未按字母顺序排序,但 html 下拉列表中的值按字母顺序排序。你知道为什么吗?
    • 让我在最后测试一下。
    • 不,它不会自动排序。请问你能分享一下你是怎么做的吗?
    • 我认为propertyData bean 对象在内部保持它的键排序或listVar 在 jstl 期间获取它们时对其进行排序。这些是我的属性: usa=USSAAA afghanistan=AFGHANISTAN-93 并且下拉菜单按 AFGHANISTAN-93 USSAAA 的顺序显示
    • 在服务器端打印属性,然后在客户端打印它,而不像我在示例代码中那样使用选项(选择)组件。让我们看看它在哪里排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    相关资源
    最近更新 更多