如何使用struts顯示從數據庫中的數據庫獲取的數據庫下拉列表中的數據?我使用城市的數組列表完成了下拉列表的代碼,但發生了錯誤。如何使用帶有struts2的mysql在下拉列表中的數據庫中顯示從數據庫獲取的數據
HTTP Status 500 - type Exception report message description
The server encountered an internal error() that prevented it from fulfilling this request.
exception org.apache.jasper.JasperException: tag 'select', field 'list', name
'location': The requested list key '%{city}' could not be resolved
as a collection/array/map/enumeration/iterator type.
我已經這樣做了代號爲城市名單如下:
JAVA代碼
public class Event extends ActionSupport{
private String description;
public List<String> city;
public List<String> getCity() {
return city;
}
public void setCity(List<String> city) {
this.city = city;
}
public String execute() throws Exception{
String url = "jdbc:mysql://localhost:20976";
String dbName = "chetan";
String driverName = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root121";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url + dbName, user,pass);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
rs = stmt.executeQuery("select * from City");
while (rs.next()) {
city.add(rs.getString("Location"));
}
return SUCCESS;
}
JSP代碼爲市
<s:select name="location" label="Location" headerValue="Select City" list="city" />
<s:submit value="Submit" method="execute" key="submit" align="center" />
你嘗試了什麼,你卡在哪裏? – 2012-07-31 09:33:41
我已經使用城市的數組列表完成了下拉列表的代碼,但發生了錯誤。 – Chetan 2012-07-31 09:38:26
請發佈帶有錯誤消息的代碼 – 2012-07-31 09:41:49