我正在构建一个使用SQL数据库显示不同赛车犬信息的应用程序。使用MVC,我有一个连接数据库的模型,并将所有Dog对象的列表发送到Controller。然后,它由Controller发送到视图,并显示在主页上。 The Homepage
程序的这一部分很好用,这是因为Controller从JSP获得了一个名为“ theCommand”的参数,但是如果该参数为null,则最初设置为显示所有狗。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// Read the command parameter
String theCommand = request.getParameter("command");
if (theCommand == null) {
theCommand = "VIEW_DOGS";
}
switch (theCommand) {
case "VIEW_DOGS":
listDogs(request, response);
break;
case "VIEW_JUMPERS":
listJumpers(request, response);
break;
case "VIEW_GAMBLERS":
listGamblers(request, response);
break;
case "VIEW_TITLING":
listTitling(request, response);
break;
default:
listDogs(request,response);
}
listJumpers(request,response);
} catch (Exception e) {
throw new ServletException();
}
}
当用户单击“查看跳线”按钮时,它会转到另一个JSP页面,该页面应该显示跳线类别中的所有狗,这与首页类似。但是,即使我的“ getJumpers”方法将跳线列表发送到控制器,JSP上也没有显示任何内容。
我知道这是因为我没有向doGet方法发送命令,而doGet方法又没有向JSP发送信息。我想知道在哪里以及如何将我的“命令”参数正确发送到控制器。
这是我的两个JSP页面
主页:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>Dog Competition Manager</h2>
</div>
</div>
<div id="container">
<div id="content">
<input type="button" value="View Jumpers" onclick="window.location.href='view-jumpers.jsp'; return false;" class="add-student-button">
<input type="button" value="View Gamblers" onclick="window.location.href='view-gamblers.jsp'; return false;" class="add-student-button">
<input type="button" value="View Titling" onclick="window.location.href='view-titling.jsp'; return false;" class="add-student-button">
<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<c:forEach var="tempDog" items="${DOG_LIST}">
<tr>
<td>${tempDog.id}</td>
<td>${tempDog.name}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
跳转页面:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css">
</head>
<body>
enter code here
<div id="wrapper">
<div id="header">
<h2>Jumpers List</h2>
</div>
</div>
<div id="container">
<div id="content">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Run Time (s)</th>
<th>Penalties (s)</th>
</tr>
<c:forEach var="tempDog" items="${JUMPERS_LIST}">
<tr>
<td>${tempDog.id}</td>
<td>${tempDog.name}</td>
<td>${tempDog.runtime}</td>
<td>${tempDog.penalty}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>