【问题标题】:How display data fetched from database on a jsp page in dropdown list using struts2 with mysql如何使用struts2和mysql在下拉列表中的jsp页面上显示从数据库中获取的数据
【发布时间】:2012-07-31 09:33:15
【问题描述】:

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

【问题讨论】:

  • 你尝试了什么,你卡在哪里了?
  • 我已经使用城市的数组列表完成了下拉列表的代码,但发生了错误。
  • 请贴出错误信息的代码
  • HTTP 状态 500 - 类型异常报告消息描述 服务器遇到内部错误 () 阻止它完成此请求。异常 org.apache.jasper.JasperException:标记“选择”、字段“列表”、名称“位置”:请求的列表键“%{city}”无法解析为集合/数组/映射/枚举/迭代器类型.
  • 和代码?

标签: java mysql jsp struts2


【解决方案1】:

在添加列表项之前初始化arrayList

city = new ArrayList<String>();

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

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

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

【讨论】:

  • 因为 headerKey 无论是否存在都不会为此发出问题。对于位置,我在 jsp 中也完成了相同的代码,也在其他 jsp 中我为列表完成了 2 个 jsp 代码
  • 例外情况是名称为 location 的选择。你也可以显示该代码吗?
  • 对于位置:
  • 在添加项目之前初始化arrayList。在将任何项目添加到列表city = new ArrayList&lt;String&gt;()之前放置它。看看here
猜你喜欢
  • 1970-01-01
  • 2017-09-19
  • 2012-02-04
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2023-03-03
相关资源
最近更新 更多