如何使用带有mysql的struts2在下拉列表中的jsp页面上从数据库中获取显示数据

问题描述 投票:0回答:1

如何使用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" />
java mysql jsp struts2
1个回答
0
投票

在附加列表中的项之前初始化arrayList

city = new ArrayList<String>();

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.