我正在尝试使用jsp servlet的MVC。但无法通过bean返回数据库中的值。
以下是我的代码:
JSP页面:
<c:forEach var="pro" items="list">
<tr>
<td><c:out value="${pro.reqno}"></c:out></td>
<td><c:out value="${pro.leave_Type}"></c:out></td>
<td><c:out value="${pro.no_of_days}"></c:out></td>
<td><c:out value="${pro.status}"></c:out></td>
<td><c:out value="${pro.balanceUnits}"></c:out></td>
<td><c:out value="${pro.totalUnits}"></c:out></td>
</tr>
</c:forEach>
Servlet :
SummaryDetail summary=new SummaryDetail();
ArrayList temp=new ArrayList();
temp=summary.getSummary();
request.setAttribute("list", temp);
RequestDispatcher rd = request.getRequestDispatcher("JSP page");
rd.forward(request, response);
DAO课程:
public ArrayList<LeaveSummary> getSummary(){
ArrayList<LeaveSummary> rowArray = new ArrayList<LeaveSummary>();
try{
LeaveSummary getValuesFromDb=new LeaveSummary();
Connection con = ConnectionProvider.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT REQNO,USERNAME,LEAVEUNITS,LEAVETYPE,STATUS,TOTALUNITS,BALANCEUNITS FROM leave_request");
while(rs.next())
{
getValuesFromDb.setReqno(rs.getInt(1));
getValuesFromDb.setUser_name(rs.getString(2));
getValuesFromDb.setNo_of_days(rs.getInt(3));
getValuesFromDb.setLeave_Type(rs.getString(4));
getValuesFromDb.setStatus(rs.getString(5));
getValuesFromDb.setTotalUnits(rs.getInt(6));
getValuesFromDb.setBalanceUnits(rs.getInt(7));
rowArray.add(getValuesFromDb);
}
}
catch(Exception e)
{}
return rowArray;
}
Bean类:
public class LeaveSummary {
public int getBalanceUnits() {
return balanceUnits;
}
public void setBalanceUnits(int balanceUnits) {
this.balanceUnits = balanceUnits;
}
public String getLeave_Type() {
return leave_Type;
}
public void setLeave_Type(String leave_Type) {
this.leave_Type = leave_Type;
}
public int getNo_of_days() {
return no_of_days;
}
public void setNo_of_days(int no_of_days) {
this.no_of_days = no_of_days;
}
public int getReqno() {
return reqno;
}
public void setReqno(int reqno) {
this.reqno = reqno;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getTotalUnits() {
return totalUnits;
}
public void setTotalUnits(int totalUnits) {
this.totalUnits = totalUnits;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
private int reqno;
private String leave_Type;
private int no_of_days; // represents no of days of leave that is LeaveUnits field in DB
private String user_name;
private int balanceUnits;
private String status;
private int totalUnits;
}
它会在我的JSP中打印任何值。请解释我的错误。提前谢谢!!
答案 0 :(得分:1)
我犯了控制器servlet的错误。
try {
List temp = summary.getSummary();
request.setAttribute("list", temp);
RequestDispatcher rd = request.getRequestDispatcher("JSP/login-success.jsp");
rd.forward(request, response);
}
答案 1 :(得分:0)
我认为你不是在jsp上迭代列表。请尝试以下代码。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<c:forEach items="${list}" var="pro">
<tr>
<td><c:out value="${pro.reqno}"></c:out></td>
<td><c:out value="${pro.leave_Type}"></c:out></td>
<td><c:out value="${pro.no_of_days}"></c:out></td>
<td><c:out value="${pro.status}"></c:out></td>
<td><c:out value="${pro.balanceUnits}"></c:out></td>
<td><c:out value="${pro.totalUnits}"></c:out></td>
</tr>
</c:forEach>
</table>