【问题标题】:Is it possible to store ArrayList index into Struts 2 selectbox as listkey?是否可以将 ArrayList 索引作为 listkey 存储到 Struts 2 选择框中?
【发布时间】:2012-10-25 11:48:07
【问题描述】:

我有一个 Struts2 应用程序。在 jsp 页面上,我使用选择框从数据库中填充 ArrayList:

<s:select name="country" list="countryList" listKey="countryId" listValue="countryName" headerKey="0" headerValue="Country" label="Select country" />

但我需要在 listKey 中存储的不是 id,而是 ArrayList 的索引。于是我读了这个索引,在 ArrayList 中找到合适的国家并检索它。有可能吗?

【问题讨论】:

    标签: java jsp struts2 arraylist


    【解决方案1】:

    尝试使用countryList.indexOf(country),如下所示:

    <s:select name="country" list="countryList" listKey="countryList.indexOf(country)" 
                                               listValue="countryName" headerKey="0"  
                                       headerValue="Country" label="Select country"/>
    

    【讨论】:

    • @user1788867:很高兴知道,这很有帮助。请不要忘记接受答案。
    • 请注意,虽然这可能无关紧要,但这是一项相当昂贵的操作,因为它会遍历列表以查找国家/地区。 IMO最好在提交后查找,只执行一次查找,或者在Java端创建索引/国家列表等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多