小编典典

MVC,Servlet,JSP,无输出

jsp

我正在尝试学习Servlet /
JSP。当我尝试以下代码时,我没有得到任何输出。有人可以帮帮我吗?当我运行代码时,我只得到标题,而下拉列表中没有显示这些值。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class VacationTrackerDAO {

public static List<VactionTrackerBean> list() throws SQLException{
    List<VactionTrackerBean> appr= new ArrayList<VactionTrackerBean>();

    try{

        DBConnection conObj=new DBConnection(); 
        Connection dbCon= conObj.getCon();
        Statement stmt=conObj.getStmt();
        String queryCPList="select * from Capacity_Plan";
        String queryApprList="Select First_Last_Name from Vacation_Approvers";
        PreparedStatement preStmtCPList=dbCon.prepareStatement(queryCPList);//get metadat
        PreparedStatement preStmtApprList=dbCon.prepareStatement(queryApprList);//get names
        //ResultSet rsCPList=preStmtCPList.executeQuery();
        ResultSet rsApprList=preStmtCPList.executeQuery();

        //ResultSetMetaData metaCPList=rsCPList.getMetaData();

        VactionTrackerBean vtBean=new VactionTrackerBean();
        while(rsApprList.next()){
            vtBean.setApprover((rsApprList.getString("First_Last_Name")));
            appr.add(vtBean);
        }


}catch(Exception e){
    System.out.println("In the Vacation TrackerDAO.java class:"+e);
}
return appr;

}
}

豆:

 public class VactionTrackerBean {

private String dates;
private String approver;
public String getDate() {
    return dates;
}
public void setDate(String dates) {
    this.dates = dates;
}
public String getApprover() {
    return approver;
}
public void setApprover(String approver) {
    this.approver = approver;
}



}

Servlet:

public class VacationTracker extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    doPost(request, response);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    try{


    List<VactionTrackerBean> vacTracker=VacationTrackerDAO.list();
    request.setAttribute("vacTracker", vacTracker);
    request.getRequestDispatcher("WEB-INF/VacationTracker.jsp").forward(request,response);

    }catch (SQLException e){
        System.out.println("Error in VacationTracker.java due to VactionTrackerDA.java:"+e);
        request.getRequestDispatcher("WEB-INF/Error.jsp").forward(request,response);
    }

}
}

JSP页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!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=ISO-8859-1">
<title>Vacation Tracker</title>  
</head>
<body>

<h1> Vacation Tracker </h1>

 <div id="main">

<c:forEach items="${vacTracker}" var="vacTracker"> 
<select id="myselect" name="mydata" size="10"> 
<option value="${vacTracker.approver}">${vacTracker.approver}</option>
</select>   
</c:forEach>

</div>


</body>
</html>

阅读 230

收藏
2020-06-10

共1个答案

小编典典

检查您是否在列表中获取值。在DAO类的while循环后放置一些SOP。

另外,代码中有一个错误。它应该是

<select id="myselect" name="mydata" size="10"> 
<c:forEach items="${vacTracker}" var="vacTracker"> 
  <option value="${vacTracker.approver}">${vacTracker.approver}</option>
</c:forEach>
</select>

否则,它将为每个项目创建新的选择。

2020-06-10