我的程序运行没有报错,但是页面不跳转?
package com.imooc.jdbc.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.jdbc.bean.Message; import com.imooc.jdbc.service.MessageService; /** * 消息列表Servlet * @author gongxiaoxiao * */ public class MessageListServlet extends HttpServlet{ private MessageService messageService; @Override public void init() throws ServletException { super.init(); messageService = new MessageService(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 分页功能 */ String pageStr = request.getParameter("page"); //当前页码 int page = 1; //默认页码为1 if (null != pageStr && (!"".equals(pageStr))) { try { page = Integer.getInteger(pageStr); } catch (Exception e) { // TODO: handle exception } List<Message> messages = messageService.getMessages(page, 5); //获得分页查询留言 int count = messageService.countMessages(); int last = (count % 5 == 0) ? (count / 5) : (count / 5 + 1); //最后一页是第几页 request.setAttribute("messages", messages); request.setAttribute("last", last); request.setAttribute("page", page); request.getRequestDispatcher("/WEB-INF/views/biz/message_list.jsp").forward(request, response);;//返回页面? } } @Override public void destroy() { super.destroy(); messageService = null; } } package com.imooc.jdbc.service; import java.util.List; import com.imooc.jdbc.bean.Message; import com.imooc.jdbc.dao.MessageDAO; public class MessageService { private MessageDAO messageDAO; public MessageService() { messageDAO = new MessageDAO(); } /** * 分页查询全部留言 * @param page 当前页码 * @param pageSize 每页记录数 * @return */ public List<Message> getMessages(int page, int pageSize){ return messageDAO.getMessages(page, pageSize); } /** * 计算留言总数 * @return */ public int countMessages(){ return messageDAO.countMessages(); } } package com.imooc.jdbc.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.imooc.jdbc.bean.Message; import com.imooc.jdbc.common.ConnectionUtil; public class MessageDAO { /** * 分页查询全部留言 * @param page 当前页码 * @param pageSize 每页记录数 * @return */ public List<Message> getMessages(int page, int pageSize){ Connection conn = ConnectionUtil.getConnection(); PreparedStatement stat = null; ResultSet set = null; List<Message> messages = new ArrayList<>(); String sql = "select * from message order by create_time desc limit ?, ?";//limit m,n 从第m条数据取一共n条记录 try { stat = conn.prepareStatement(sql); stat.setInt(1, (page - 1)*pageSize); stat.setInt(2, pageSize); set = stat.executeQuery(); while (set.next()) { messages.add(new Message(set.getLong("id"), set.getLong("userId"), set.getString("content"), set.getString("title"), set.getString("username"), /* rs.getDate()只是返回日期部分 java.sql.Date rs.getTime()只是返回时间部分 java.sql.Time rs.getTimestamp()才是返回时间和日期 java.sql.Timestamp*/ set.getTimestamp("create_time"))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { ConnectionUtil.release(set, stat, conn); } return messages; } /** * 计算留言总数 * @return */ public int countMessages() { Connection conn = ConnectionUtil.getConnection(); PreparedStatement stat = null; ResultSet set = null; String sql = "select count(*) total from message"; try { stat = conn.prepareStatement(sql); set = stat.executeQuery(); return set.getInt("total"); } catch (SQLException e) { e.printStackTrace(); }finally { ConnectionUtil.release(set, stat, conn); } return 0; } } <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>JDBCMessageBoard</display-name> <servlet> <servlet-name>MessageListServlet</servlet-name> <servlet-class>com.imooc.jdbc.servlet.MessageListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MessageListServlet</servlet-name> <url-pattern>/message/list.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location> </error-page> </web-app> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> This is a index.html! </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; pageContext.setAttribute("basePath", basePath); %> <html> <head> <meta charset="UTF-8"> <title>留言板</title> <link rel="stylesheet" href="${pageScope.basePath}/css/index.css"> <script type="text/javascript"> function submitMessageForm(flag) { if ('first' == flag) { document.getElementById('page').value = 1; } else if ('pre' == flag) { var current = Number(document.getElementById('page').value); if (current > 1) { document.getElementById('page').value = current - 1; } } else if ('next' == flag) { var current = Number(document.getElementById('page').value); var last = Number(document.getElementById('last').value); if (current < last) { document.getElementById('page').value = current + 1; } } else if ('last' == flag) { var last = Number(document.getElementById('last').value); document.getElementById('page').value = last < 1 ? 1 : last; } document.getElementById('messageForm').submit(); } </script> </head> <body> <header> <div class="container"> <% if (null != request.getSession().getAttribute("user")) {%> <nav> <a href="${pageScope.basePath}/my/message/list.do">我的留言</a> </nav> <nav> <a href="${pageScope.basePath}/userInfo.do">我的信息</a> </nav> <%} else { %> <nav> <a href="${pageScope.basePath}/login.do">登录</a> <a href="${pageScope.basePath}/regPrompt.do">注册</a> </nav> <% } %> </div> </header> <section class="banner"> <div class="container"> <div> <h1>慕课网留言板</h1> <p>慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。 </p> </div> </div> </section> <section class="main"> <div class="container"> <c:forEach items="${messages}" var="msg"> <div class="alt-item"> <div class="alt-head"> <div class="alt-info"> <span>作者:<a href="">${msg.username}</a></span> <span>时间:<fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${msg.createTime}"/></span> </div> </div> <div class="alt-content"> <h3>${msg.title}</h3> <p>${msg.content}</p> </div> </div> </c:forEach> </div> </section> <section class="page"> <div class="container"> <% if (null != request.getSession().getAttribute("user")) {%> <div id="fatie"> <a href="${pageScope.basePath}/addMessagePrompt.do"><button>点我留言</button></a> </div> <%} else { %> <div id="fatie"> 请<a href="${pageScope.basePath}/login.do"><button>登录</button></a>后留言 </div> <% } %> <div id="pagefy"> <ul> <form id="messageForm" action="${pageScope.basePath}/message/list.do" method="post"> <input type="hidden" id="page" name="page" value="${page}"> <input type="hidden" id="last" name="last" value="${last}"> <li><a href="javascript:void(0)" onclick="submitMessageForm('first')">首页</a></li> <li><a href="javascript:void(0)" onclick="submitMessageForm('pre')">上一页</a></li> <li><a href="javascript:void(0)">当前第${page}页</a></li> <li><a href="javascript:void(0)" onclick="submitMessageForm('next')">下一页</a></li> <li><a href="javascript:void(0)" onclick="submitMessageForm('last')">尾页</a></li> </form> </ul> </div> </div> </section> <footer> copy@慕课网 </footer> </body> </html>
0
收起
正在回答
10回答
同学可以参考一下上图中老师的代码,同学是不算把所有跳转的代码都放到if里 了?
第一次pagestr是没有值的,进不去if语句的,所以不要把跳转的页面都放在if中
应该把下图红框部分的代码放到if外边:
如果我的回答解决了你的问题,请采纳,祝学习愉快.
慕数据0284452
2019-02-25 19:46:33
刚才看到login.jsp里面有个小错误,多了个符号,请老师将上传的eclipse版本的留言板项目再检查下~
慕数据0284452
2019-02-25 19:22:15
一叶老师,我这边按您的修改方式出现这样的问题
1.样式出现混乱
2.点击翻页会报错
慕数据0284452
2019-02-25 15:38:33
是不是因为我的form表单这里有报错导致不触发?message_list.jsp是直接从老师的eclipse项目copy的
慕数据0284452
2019-02-24 20:19:47
慕数据0284452
2019-02-23 20:22:19
这里也改掉了,还是显示页面空白
慕数据0284452
2019-02-23 19:03:17
刚才自己又检查了下,发现web.xml放在view目录下,现在移动到了WEB-INF目录下,再运行就这样了,,
慕数据0284452
2019-02-23 18:54:41
是这个图
慕数据0284452
2019-02-23 18:52:58
修改后的index.jsp,运行能跳到list.do但还是404,且
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星