![](/img/trans.png)
[英]how to count number of records found from database and display in jsp page using struts2?
[英]How display data fetched from database on a jsp page in dropdown list using struts2 with mysql
如何使用struts在下拉列表中顯示從JSP上的數據庫中獲取的數據? 我已經使用城市的數組列表完成了下拉列表的代碼,但發生了錯誤。
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" />
在添加列表中的項目之前初始化 arrayList
city = new ArrayList<String>();
在您的操作中,您已將列表名稱聲明為city
,因此在您的選擇中, list
屬性也應相同
<s:select label="City List" headerKey="-1" list="city" name="whatever" />
根據我的知識, name
屬性也必須沒有括號,並且應該與您在提交操作中聲明的變量名稱匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.