无法使用<c:foreach>输出ArrayList元素

时间:2017-11-04 00:30:46

标签: java jsp servlets arraylist

我在网站上看过很多类似的问题,但我无法将我的ArrayList中的项目打印到我的JSP上

我已经测试过,我知道我的ArrayList是从我的查询中填充的。在业务类和servlet / controller中都是

有人可以告诉我我在哪里犯了错误

我的商务课程

public class Drug implements Serializable{

private long drugID;
private String drugName;
private String size;

public Drug(){
    drugName="";
    size=""; 
}

public Drug(long id,String name,String dSize){
    drugID=id;
    drugName=name;
    size=dSize; 
}

//Setters
public void setDrugID(long drugID){
    this.drugID=drugID;
}
public void setDrugName(String drugName){
    this.drugName=drugName;
}

public void setSize(String size){
    this.size=size;
}

 //Getters
public Long getDrugID(){
    return drugID;
}
public String getDrugName(){
    return drugName;
}

public String getSize(){
    return size;
}

 }//EOC


 public class DrugDB {
private final static Logger log = LogManager.getLogger(DrugDB.class);

 /*
    Method to process the search for a drug from the table
    based on user imputting the name of the drug or part of
    using LIKE with a limit of 20
 */

 public static ArrayList<Drug> searchDrugs(String drugN) {
    ConnectionPool pool = ConnectionPool.getInstance();
    Connection connection = pool.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;

    String query = "SELECT * FROM drugs where dname LIKE ? LIMIT 20";
    try {
        System.out.println("Trying query now");
        ps = connection.prepareStatement(query);
        ps.setString(1, drugN + "%");
        rs = ps.executeQuery();
        ArrayList<Drug> dsearch = new ArrayList<>();
        //System.out.println("results "+rs.next());
        while (rs.next()) {
            Drug d = new Drug();
            d.setDrugID(rs.getLong("drugid"));
            d.setDrugName(rs.getString("dname"));
            d.setSize(rs.getString("dsize"));
            dsearch.add(d);   
           // System.out.println("Adding items");
        }
        return dsearch;
    } catch (SQLException e) {
        System.err.println(e);
        log.error("Exception when trying to search for a Drug",e);
        return null;
    } finally {
        DBUtil.closeResultSet(rs);
        DBUtil.closePreparedStatement(ps);
        pool.freeConnection(connection);
    }
}


}//Eoc DrugDB

我的Servlet

 public class ScheduleController extends HttpServlet{
 final static Logger log = LogManager.getLogger(ScheduleController.class);

 @Override
  public void doPost(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
    String requestURI = request.getRequestURI();
    String url = "";
    // open Drug Search page
    if (requestURI.endsWith("/searchDrugs")) {
        url = searchDrugs (request, response);
    } 
    // Search for a drug
    if (requestURI.endsWith("/doDrugSearch")) {
        url = doDrugSearch (request, response);
    }        
    getServletContext()
            .getRequestDispatcher(url)
            .forward(request, response);
}

private String searchDrugs(HttpServletRequest request,
        HttpServletResponse response) {

    String url ="/sched/searchdrugs.jsp";        
            return url;     
}
private String doDrugSearch(HttpServletRequest request,
        HttpServletResponse response) {

    String url;        
    String message;
    // get values from form
    String dName = request.getParameter("drugName");


    //validate the values to check for empty values in case JS registration check has failed.
    if(dName.length()==0){
        message="You have not filled out the required fields.";
        request.setAttribute("messate", message);
        url = "/sched/searchdrugs.jsp";
    }else{

        //get the results
            ArrayList<Drug> sresults;
           sresults= DrugDB.searchDrugs(dName);
           //int i = sresults.size();
           //System.out.println("searc size is "+i);

// set the session variable to hold the arraylust
           HttpSession session = request.getSession();
           session.setAttribute("drugSearchResults",sresults);
           url="/sched/drugsearchresults.jsp";
    }
  return url;
}// Eof doDrugSearch

} // EOC ScheduleController

我的JSP的适当部分

 <table>
     <tr valign="bottom">
    <th></th>
 <th>Drug Name</th>
 <th>Size</th>
 </tr>
 <c:forEach var="drug" items="${SessionScope.drugSearchResults}">
<tr valign="top">
<td>${drug.drugID}</td>
<td>${drug.drugName}</td>
<td>>${drug.size}</td>
 </tr>
</c:forEach>  
</table>

1 个答案:

答案 0 :(得分:0)

 <c:forEach var="drug" items="${SessionScope.drugSearchResults}">

SessionScope应为sessionScope