我一直致力于一个连接到填充指定字段的数据库的Web应用程序。它基本上是netbeans tutorial的编辑。好吧,我已经设置了数据库并通过在响应页面上填充所有信息的表来测试它。当我从列表中选择要查看的标题之一时,index.jsp会出现问题。我使用标题而不是ID号来选择书籍:
SELECT titlefk FROM BookList
而不是:
SELECT id, titlefk FROM BookList
因为下拉框会显示ID和Title作为选项。选择书名并单击提交键后,它应使用数据库中的相应信息填充下一页。
网站图片: http://imgur.com/a/kVEMN
上表是检查数据库是否已连接的临时措施。我在数据库中选择了第三个选项,但我的response.jsp继续填充第一本书的信息。我不确定我在代码中缺少什么,但我希望有人能指出我正确的方向。
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<sql:query var="books" dataSource="jdbc/IFPBOOKLISTRT">
SELECT titlefk FROM BookList
</sql:query>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>IFPBOOKLIST Homepage</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Welcome to IFPBOOKLIST, My Personal Book Selection</h1>
<table border="0">
<thead>
<tr>
<th>Here you will find my personal books to-do list</th>
</tr>
</thead>
<tbody>
<tr>
<td>To view details, select a book below</td>
</tr>
<tr>
<td><form action="response.jsp">
<strong>Select a book:</strong>
<select name="subject_id">
<c:forEach var="row" items="${books.rowsByIndex}">
<c:forEach var="column" items="${row}">
<option value="<c:out value="${column}"/>"><c:out value="${column}"/></option>
</c:forEach>
</c:forEach>
</select>
<input type="submit" value="submit" name="submit" />
</form>
</td>
</tr>
</tbody>
</table>
</body>
这是response.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<sql:query var="bookQuery" dataSource="jdbc/IFPBOOKLISTRT">
SELECT * FROM BookList,Book
WHERE Book.id=BookList.id
</sql:query>
<c:set var="bookDetails" value="${bookQuery.rows[0]}"/>
<table border="1">
<!-- column headers -->
<tr>
<c:forEach var="columnName" items="${bookQuery.columnNames}">
<th><c:out value="${columnName}"/></th>
</c:forEach>
</tr>
<!-- column data -->
<c:forEach var="row" items="${bookQuery.rowsByIndex}">
<tr>
<c:forEach var="column" items="${row}">
<td><c:out value="${column}"/></td>
</c:forEach>
</tr>
</c:forEach>
</table>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>${bookDetails.title}</title>
</head>
<body>
<table border="0">
<thead>
<tr>
<th colspan="2">${bookDetails.title}</th>
</tr>
<tr>
<td>Edition: <a href="placeholder">${bookDetails.edition}</a></td>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong>Description: </strong>
</td>
<td>
{placeholder}
</td>
<link rel="stylesheet" type="text/css" href="style.css">
</tr>
<tr>
<td>
<strong>Publisher </strong>
</td>
<td>
${bookDetails.publisher}
</td>
</tr>
<tr>
<td>
<strong>
Published Date
</strong>
</td>
<td>
${bookDetails.datePublished}
</td>
</tr>
<tr>
<td>
<strong>Author </strong>
</td>
<td>
${bookDetails.author}
</td>
</tr>
<tr>
<td>
<strong>isbnNumber </strong>
</td>
<td>
<a href="placeholder">${bookDetails.isbnNumber}</a>
<br>
</td>
</tr>
<tr>
<td>
<strong>Amazon Link: </strong></td>
<td>
<a href="placeholder">${bookDetails.amazonLink}</a>
<br>
</td>
</tr>
</tbody>
</table>
</body>
答案 0 :(得分:0)
你错过的很简单。你应该能够很容易地将它塑造成形状。
您永远不会阅读所选的选项,请在response.jsp中查看您的查询实际上是拉出所有书籍,然后选择数据库返回的第一个书籍(例如rows [0]):
<sql:query var="bookQuery" dataSource="jdbc/IFPBOOKLISTRT">
SELECT * FROM BookList,Book
WHERE Book.id=BookList.id
</sql:query>
<c:set var="bookDetails" value="${bookQuery.rows[0]}"/>
你在response.jsp中需要做的是:
1)读取index.jsp传入的subject_id参数。
2)使用subject_id过滤(通过SQL WHERE子句)bookQuery,以便只返回所选书籍。
回到你的教程,如果仔细看看接近结尾,在response.jsp下面的第3项,你会看到他们是如何做到的:
<sql:query var="counselorQuery" dataSource="jdbc/IFPWAFCAD">
SELECT * FROM Subject, Counselor
WHERE Counselor.counselor_id = Subject.counselor_idfk
AND Subject.subject_id = ? <sql:param value="${param.subject_id}"/>
</sql:query>
希望这有帮助!