-2

我的jsp文件是:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@ page import="com.elc.util.*" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Select Your Department</h1>
<s:form action="Comapny">
<table bordercolor="red">
<tr>
    <td>Select your Option</td>
    <td>
    <s:combobox name ="Depart"list="%{depat}" value="Select" headerValue="-1"    
 headerKey="select your option"></s:combobox>
    </td>
 </tr>
 <tr>
    <td></td>
    <td>
    <s:submit value="Select"></s:submit>
    </td>
 </tr>

 </table>

</s:form>
</body>
</html>

A而我的动作课是...

package com.elc.action;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Department extends ActionSupport {
private String Dept;
private List<Object> depat;

public List<Object> getDepat() {
    return depat;
}

public void setDepat(List<Object> depat) {
    this.depat = depat;
}

public String getDept() {
    return Dept;
}

public void setDept(String dept) {
    Dept = dept;
}
//@SuppressWarnings("unchecked")
public String Select(){
        Collection<String> o = DBConnection.getConnection();
depat = new ArrayList<Object>();
Iterator<String> i = o.iterator();
while(i.hasNext()){
    depat.add(i);

}
System.out.println("the list contains..........."+depat);
return "success";
}
//@SuppressWarnings("static-access")
public String execute(){
    return SUCCESS;
}

   }

我的 DAO 课程是.......

  package com.elc.action;

  import java.sql.*;
  import java.util.ArrayList;
  import java.util.Collection;
  public class DBConnection {
public static Collection<String> getConnection(){
    ArrayList<String> l = new ArrayList<String>();
    try{
        Class.forName(DBConstants.Driver);
        Connection con = DriverManager.getConnection(DBConstants.url,         
                         DBConstants.userName, DBConstants.password);
        Statement st = con.createStatement();
        String query = DBConstants.SELECT +"* from role";
        ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            rs.getInt(1);
            String dept = rs.getString(2);
            l.add(dept);
        }

    }catch(ClassNotFoundException c){
        System.out.println("the classs u are asking not found");
        c.printStackTrace();
    }catch(SQLException s){
        System.out.println("the sql exception is occured");
        s.printStackTrace();
    }
    return l;
}

   }

首先,我在我的 DBConnection.getconnection 方法中使用了 Object 的返回,它以 [HR,Tester,Software Engineer] 检索了我的表,但是当我用来在我的 Web 中下拉时,它们按原样出现......即 [HR,Tester , 软件工程师] 所以我希望输出为该测试人员的第一个 Hr 及以下以及该软件工程师的以下。当我在 DBConnection.getconnection 方法中使用 Collection 或 List 返回时,我得到了 jasper 异常..

有没有人告诉我答案。。。。。。

4

2 回答 2

0

我的jsp没有任何错误,我改变了我的Action类,它只是我现在发布的......剩下的程序只是......

public class Department extends ActionSupport {

 @SuppressWarnings("rawtypes")
ArrayList deptList;
@SuppressWarnings("rawtypes")
public ArrayList getDeptList() {
    return deptList;
}

@SuppressWarnings("rawtypes")
public void setDeptList(ArrayList deptList) {
    this.deptList = deptList;
}


public String Select(){
         deptList = DBConnection.getConnection();
System.out.println("the list contains..........."+deptList);
return "checked";
}
public String execute(){
    return SUCCESS;
}

    }

首先,我创建了一个 Collection 接口,我没有该变量的 getter 和 setter。现在我创建了一个 Arraylist 并拥有 getter 和 setter,现在它工作正常.....

于 2012-11-30T11:41:26.453 回答
0
public class Mainlist {
    DBLayer db;

    public Mainlist() {
        db = new DBLayer();
    }

    public ArrayList getCategoryname() {
        ArrayList<String> list = new ArrayList();
        try {
            String sql = "select category_name from category";
            PreparedStatement ps = db.getConnection().prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    list.add(rs.getString("category_name"));
                    // System.out.println(list);
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }
    }
}
于 2013-01-18T13:28:39.763 回答