0

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

1 回答 1

0

在列表中追加项目之前初始化 arrayList

city = new ArrayList<String>();

在您的操作中,您已将列表名称声明为city,因此在您的选择中,list属性也应该相同

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

此外name,据我所知,该属性必须不带括号,并且应该与您在提交操作中声明的变量名称相匹配。

于 2012-07-31T10:07:09.010 回答