web工程数据库链接出现问题,可以查询出来数据库信息,无法在jsp界面显示
dao: public List findall();
daoimpl: public List findall() {
//使用JDBC操作数据库...
//1.定义sql
String sql = "select * from only where event in not null";
List only = template.query(sql, new BeanPropertyRowMapper(Only.class));
return only;
}
service: public List findall();
serviceimlp: public List findall() {
//调用Dao完成查询
return dao.findall();
}
servlet: //获取条件查询参数
Map condition = request.getParameterMap();
//2.调用service查询
OnlyService service = new OnlyServiceImpl();
PageBean pb = service.findUserByPage(currentPage,rows,condition);
System.out.println(pb);
//3.将PageBean存入request
request.setAttribute("pb",pb);
request.setAttribute("condition",condition);//将查询条件存入request
//4.转发到list.jsp
request.getRequestDispatcher("/event.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
jsp: <div class="container">
"text-align: center">学生信息列表
"1"class="table table-bordered table-hover">
class="success">
class="yc">序号
姓名
事务
操作
class="yc">${only.uid}
${only.username}
${only.event}
class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findStudentServlet?uid=${only.uid}">修改
该回答引用于ChatGPT:
根据您提供的代码,您的 DAO 层通过 JDBC 连接到数据库并查询数据成功。但是在 JSP 界面上无法显示您查询的结果。
在您提供的代码中,您调用了一个 findUserByPage
方法,该方法应该返回一个 PageBean
对象,而不是 List<Only>
对象。如果您想返回 List<Only>
对象,请修改该方法的返回类型为 List<Only>
。
此外,从您提供的 JSP 代码中看不到任何与 Only
对象列表渲染相关的内容。您需要使用 JSP 标签库或 EL 表达式将 List<Only>
对象中的每个对象提取并在 HTML 表格中渲染出来。
以下是一个可能的 JSP 页面示例,可以帮助您渲染 List<Only>
中的对象:
<div class="container">
<h3 style="text-align: center">学生信息列表</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th class="yc">序号</th>
<th>姓名</th>
<th>事务</th>
<th>操作</th>
</tr>
<c:forEach items="${onlyList}" var="only">
<tr>
<td>${only.uid}</td>
<td>${only.username}</td>
<td>${only.event}</td>
<td><a class="btn btn-default btn-sm" href="<c:url value='/findStudentServlet'/>?uid=${only.uid}">修改</a></td>
</tr>
</c:forEach>
</table>
</div>
请注意,该示例中使用了 JSTL 标签库来简化页面开发。您需要在 JSP 页面的开头引入以下代码才能使用该标签库:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
希望这些信息对您有所帮助。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
根据提供的代码和问题描述,可以初步判断是查询数据成功了,但是在 JSP 页面中没有正确显示数据。可能的原因有以下几个方面:
假设在 JSP 页面中使用了类似以下的语句进行数据的输出:
<td class="yc">${only.uid}
${only.username}
${only.event}
这里使用的是 EL 表达式,但是在使用时需要遍历 List 集合中的所有元素,如果没有正确遍历数据集合,只输出了一个元素,就会导致数据显示不全。
可以将这部分代码改为以下这种形式:
<c:forEach items="${pb.list}" var="only">
<tr>
<td>${only.uid}</td>
<td>${only.username}</td>
<td>${only.event}</td>
<td>…</td>
</tr>
</c:forEach>
这样就可以正确输出整个 List 集合中的数据了。
在 Servlet 中传递数据到 JSP 页面时,使用了以下语句:
request.setAttribute("pb",pb);
在 JSP 页面中可以通过 EL 表达式获取该变量的值,例如:
${pb.pageNumber}
${pb.pageSize}
${pb.totalCount}
${pb.totalPage}
${pb.list}
但是如果传递的变量名与 JSP 页面中使用的 EL 表达式名称不一致,就会导致数据无法正确显示。可以检查一下这方面的问题。
根据提供的代码,可以看到在 SQL 语句中使用了 not null
进行条件查询,这意味着在数据库中如果没有符合条件的数据,就不会有数据进行查询和显示。可以检查一下是否存在该方面的问题。
如果我的回答解决了您的问题,请采纳!
主要有2个思路:1,数据库没数据 2,代码写错了,没有数据交流
首先,应该是先判断下数据库是否有数据,其次,最好是在前后两端debug一下,看看有没有数据交流
根据您提供的代码和描述,似乎是由于在jsp界面中无法正确显示数据库查询结果导致的问题。我注意到在jsp界面中使用了${only.uid}、${only.username}和${only.event}来获取每条查询结果的uid、username和event属性,但是在代码中没有看到传递only对象或者以only对象为属性名的变量,所以jsp页面中可能无法正确解析这些表达式。
可能的解决方法如下:
在servlet中设置only属性并将其存入request中,在jsp页面中使用${requestScope.only.uid}等表达式来获取属性值。
将查询结果存储在一个List对象中,并将List对象作为属性存入request中。在jsp页面中使用JSTL标签库的<c:forEach>标签遍历List对象,并使用${only.uid}等表达式来获取每个对象的属性值。
以下是第二种方法的代码示例:
在servlet中:
//调用service查询
OnlyService service = new OnlyServiceImpl();
List<Only> onlyList = service.findall();
request.setAttribute("onlyList", onlyList);
//转发到jsp页面
request.getRequestDispatcher("/event.jsp").forward(request,response);
在jsp页面中:
<div class="container">
<h3 style="text-align: center">学生信息列表</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th class="yc">序号</th>
<th>姓名</th>
<th>事务</th>
<th>操作</th>
</tr>
<c:forEach var="only" items="${onlyList}">
<tr>
<td class="yc">${only.uid}</td>
<td>${only.username}</td>
<td>${only.event}</td>
<td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findStudentServlet?uid=${only.uid}">修改</a></td>
</tr>
</c:forEach>
</table>
</div>
希望这些代码示例能够帮助您解决问题。
该回答引用ChatGPT
request.setAttribute("pb",pb);