struts2中如何把一个从数据库取得的数据集存入到页面显示的中去
struts2中怎么把一个从数据库取得的数据集存入到页面显示的<s:combobox>中去?
最近小弟要做一个小东西,要用到<s:combobox>。先是从数据库取得一个数据集,
代码如下:
ResultSet rs=statement.executeQuery("select color from T1");
rs中的color有red,yellow等,
然后把rs(比如)包含的内容red、yellow等放到放到页面显示的<s:combobox>中去,请问怎么实现啊?
谢谢!
------解决方案--------------------
你用的是Struts2的话应该分层了啊
那么应该有实体类啦?
在循环的时候
最近小弟要做一个小东西,要用到<s:combobox>。先是从数据库取得一个数据集,
代码如下:
ResultSet rs=statement.executeQuery("select color from T1");
rs中的color有red,yellow等,
然后把rs(比如)包含的内容red、yellow等放到放到页面显示的<s:combobox>中去,请问怎么实现啊?
谢谢!
------解决方案--------------------
你用的是Struts2的话应该分层了啊
那么应该有实体类啦?
在循环的时候
- Java code
List list = new ArrayList(); while(rs.next()) { obj.setUserId(rs.getInt("userid"); obj.setUserName(rs.getString("username"); list.add(obj); } //这里将所有的对象放入list中然后传入Action中 //在页面上 <s:select name="" list="list" listKey="userid" listValue="username"></s:select>
------解决方案--------------------
list 属性中放数据源 例如 #request.users
listKey 放的那个属性你需要放做 OPTION 中的value,
listValue 放SELECT显示给用户看的数据名称
例如:
struts2 tag:
<s:select name="" list="users" listKey="userId" listValue="username"></s:select>
html:
<select>
<option value="volvo">Volvo</option>
</select>
list: true false String Iteratable source to populate from. If this is missing, the select widget is simply not displayed.
listKey: false false String Set the key used to retrive the option key.
listValue: false false String Set the value used to retrive the option value.