我目前正在开发一个包含 JSP、servlet 和一些 CRUD 数据库服务的网站。我陷入了 CRUD 的读取部分,因为我无法获得所需的效果。
我已连接到数据库,并且能够毫无问题地从网站插入数据。我现在想做的是在 JSP 调用中对数据库中的一行有一个选择/选项框,并在下一页上翻译该行的相应列。但是,我无法从选项框中显示多于一行。代码如下。
Bean 类:
public class ReadBean {
protected String title;
protected String author;
protected String text;
public String getTitle() {
return title;
}
public void setTitle(String newTitle) {
title = newTitle;
}
public String getAuthor() {
return author;
}
public void setAuthor(String newAuthor) {
author = newAuthor;
}
public String getText() {
return text;
}
public void setText(String newText) {
text = newText;
}
}
Controller :
@WebServlet(urlPatterns = { "/com/defaultValidate/ReadController" })
public class ReadController extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ReadBean reader = new ReadBean();
String address;
if (request.getParameter("ReadConfirm") != null) {
try {
Connection connect = ConnectionManager.getConnection();
String SQL = "SELECT * FROM prayers ORDER BY prayerTitle DESC";
PreparedStatement ps = connect.prepareStatement(SQL);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
request.getSession().setAttribute("ref", reader);
reader.setTitle(rs.getString("prayerTitle"));
}
} catch (Exception e) {
}
address = "ReadConfirm.jsp";
} else if (request.getParameter("ReadView") != null) {
address = "ReadView.jsp";
} else {
address = null;
}
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
然后是 Controller 响应的对应jsp页面:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="MenuHeader.jsp"%>
</div>
<div id="mainBorder"> </div>
<div id="mainBody">
<table id="mainTable">
<tr>
<td id="sidebar">
<h2>Options</h2>
<ul>
</ul>
</td>
<td id="mainContent">
<h2 id="contentHeader">Select a prayer</h2>
<form action="ReadController">
<table border="1" width="1" cellspacing="5" cellpadding="5">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><select name="prayer_id">
<c:forEach var="ReadController" items="${ref.title}">
<option value="">${ref.title}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="ReadView" value="View"> </td>
</tr>
</tbody>
</table>
</form>
<td id="imageBar">
</td>
</table>
<%@ include file="../menuHeaderAndFooter/MenuFooter.jsp" %>
我绝对不是专家,但我认为我在从 Controller 到 JSP 中的 for-each 元素的连接之间遗漏了一些东西,而我只是陷入了困境。如果好奇的话,本节的 WEB-XML 部分是可操作的,并且 ConnectionManager 是一个简单的调用来设置与数据库的连接。 Glassfish 包是所使用的服务器。
如果您对代码的任何其他部分有任何疑问,请告诉我,因为我对尽快高效地完成并运行这个特定项目非常感兴趣。我很乐意提供任何额外的编码和引用要求。
我们非常感谢在此特定地点提供的任何帮助,并且非常有帮助。感谢您查看此内容。
最佳答案
问题出在这部分代码
while(rs.next()){
request.getSession().setAttribute("ref", reader);
reader.setTitle(rs.getString("prayerTitle"));
}
每次读取新值然后将该“ref”放入 session 中时,您都会覆盖“ref”属性。因此,您在 session 中放入单个值,因此您将获得一行。
因此,请将所有值放入一个集合(如列表)中,然后在 session 中设置该列表:
List<ReadBean> myList = new Arraylist<ReadBean>();
while(rs.next()){
ReadBean reader = new ReadBean();
reader.setTitle(rs.getString("prayerTitle"));
myList.add(reader);
}
request.getSession().setAttribute("ref", myList);
现在您可以在 jsp 中使用 for every
如下:
<c:forEach var="myReader" items="${ref}">
<option value="">${myReader.title}</option>
</c:forEach>
请注意我在上面的 for-each
循环中所做的更改。
首先在 items
属性中,您需要一个集合,因此我们在 session 中设置一个名为 ref
的集合,以便我们可以使用它。
现在在var
属性中,我们定义一个局部变量,它类似于
for(Reader myReader : ref)
//myReader 将进入 var
和 ref
列表(集合)将进入 项目
现在您可以使用 for-each
中的 myReader
var 访问属性,例如 ${myReader.title}
或 ${myReader['title']}
关于java - 为 JSP/Servlet 应用程序实现 CRUD 服务 - 读取操作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14597977/