jsp页面显示不出来数据..请帮帮忙
这是servlet代码public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UsersDao ud = new UsersDaoImpl();
List<Users> lu = ud.getAllUsers();
request.setAttribute("lu", lu);
System.out.println(lu);
request.getRequestDispatcher("/ShowUsers.jsp").forward(request, response);
}
这是jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="com.zj.entity.admin.Users"%>
<%@page import="com.zj.model.admin.adminImpl.UsersDaoImpl"%>
<%@page import="com.zj.model.admin.UsersDao" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ShowUsers.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Object obj = request.getAttribute("lu");
List<Users> lu = null;
if(obj!=null){
lu = (List<Users>)obj;
%>
<table border="1" width="80%">
<tr>
<th>账户</th>
<th>密码</th>
</tr>
<%
for(Users ushow : lu) {
%>
<tr>
<td><%=ushow.getUsername() %></td>
<td><%=ushow.getPassword()%></td>
</tr>
<%
}
%>
<%
}
%>
</body>
</html>
查询方法是对的,用main方法测试的时候 数据全部出来的,请大家帮我看看 .. jsp页面打开的时候一片空白. --------------------编程问答-------------------- 你XML配置了吗?你给的代码也不全没法答 --------------------编程问答-------------------- 报异常了吗 --------------------编程问答-------------------- <%
Object obj = request.getAttribute("lu");
List<Users> lu = null;
if(obj!=null){
lu = (List<Users>)obj;
%>
这样,你现在这部分里随便输出一个什么东西看看是不是这个页面没有被执行到 --------------------编程问答-------------------- request.getRequestDispatcher("/ShowUsers.jsp").forward(request, response);
你确定这个路径没有写错?如果是正确的话,你在导航后看看客户端生成的网页源代码像不像你这个网页里生成的。 --------------------编程问答-------------------- 少了table标签 ,</table> --------------------编程问答-------------------- 我给你说说我的见解:
1、</table>缺少
2、 request.getRequestDispatcher("/ShowUsers.jsp").forward(request, response);改为:
request.getRequestDispatcher("ShowUsers.jsp").forward(request, response);
3、<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>导入
<c:forEach var="ushow" items="lu">
<tr>
<td><%=ushow.getUsername() %></td>
<td><%=ushow.getPassword()%></td>
</tr>
</c:forEach>
4、如果这个三个改了 还是还行不行的话,你就在Servlet或DAO里找错误,这个细心一点就能搞定,学会调试就可以了 --------------------编程问答-------------------- 补充一点哦 没猜错的话,你这个出结果了 我觉得有乱码的,在servlet里加上
response.setContentType("text/html;charset=gbk"); --------------------编程问答-------------------- List<Users> lu = ud.getAllUsers();
在它下面输出list的长度,看list有没有值
System.out.print(lu.size);
有值,就是jsp页面代码写的不对了 --------------------编程问答-------------------- 确实是少了 table 标签~! --------------------编程问答-------------------- 贴出异常是解决问题的最好办法 --------------------编程问答-------------------- 不好意思,我来拿分的。 --------------------编程问答-------------------- 后台debug 看看你查询出来list是否包含元素,还有就是报什么异常,要是没有异常的话 ,再去检查前台 --------------------编程问答-------------------- lu打印有值没?是跳转不到这页面还是什么问题
补充:Java , Web 开发