Struts选择标记<key,value> pair </key,value>

时间:2012-12-02 23:19:58

标签: jdbc struts2

使用Struts2,我必须显示类似

的内容
<select>
   <option value="1>India</option>
   <option value="2">US</option>
   <option value="3">Mexico</option>
</select>

现在,值“1,2,3 ......”和“印度,美国,墨西哥......”都是通过JDBC连接从数据库中获取的。我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果你有这门课程:

class Country {
    private String id;
    private String name;
    ...
}

使用JDBC恢复数据库中的所有Country对象:

List<Country> countries = CountryDAO.getAllCountries();

您可以使用<s:select>标记在jsp中呈现此列表。

动作

class Action extends ActionSupport {
    private List<Country> countries;
    ...
    public List<Country> getCountries() {...}
}

的jsp

<s:select label="Pets"
   list="countries"
   listKey="id"
   listValue="name"/>