2012-07-31 43 views
0

如何使用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" /> 
+2

你嘗試了什麼,你卡在哪裏? – 2012-07-31 09:33:41

+0

我已經使用城市的數組列表完成了下拉列表的代碼,但發生了錯誤。 – Chetan 2012-07-31 09:38:26

+1

請發佈帶有錯誤消息的代碼 – 2012-07-31 09:41:49

回答

0

初始化ArrayList中您將追加前列表中的項目

city = new ArrayList<String>(); 

在你的行動,你已經聲明列表名稱爲city,所以在您選擇list屬性也應該是相同的

<s:select label="City List" headerKey="-1" list="city" name="whatever" /> 

另外,name屬性必須是沒有括號,按我的知識和應匹配您在提交操作中聲明的變量名稱。

+0

我也做過,但同樣的錯誤得到它。 – Chetan 2012-07-31 10:11:15

+0

因爲如果它存在與否,headerKey不會發出。 位置我已經做了相同的代碼在jsp中也在其他jsp我已經做了2個jsp代碼爲列表 – Chetan 2012-07-31 10:18:55

+0

是的。這是一個正確的代碼。 – Chetan 2012-07-31 10:22:05

相關問題