代码都是照着源码写的,大佬们知道是哪里出问题了吗
首先要么你查总条数的sql没写对,要么就是查出来后没有设置到page对象中
上面service错了
是这一个:
public List<Message> queryMessageList(String command,String description,Page page) {
// 组织消息对象
Message message = new Message();
message.setCommand(command);
message.setDescription(description);
MessageDao messageDao = new MessageDao();
// 根据条件查询条数
int totalNumber = messageDao.count(message);
// 组织分页查询参数
page.setTotalNumber(totalNumber);
Map<String,Object> parameter = new HashMap<String, Object>();
parameter.put("message", message);
parameter.put("page", page);
// 分页查询并返回结果
return messageDao.queryMessageList(parameter);
}
public List<Message> queryMessageListByPage(String command,String description,Page page) {
Map<String,Object> parameter = new HashMap<String, Object>();
// 组织消息对象
Message message = new Message();
message.setCommand(command);
message.setDescription(description);
parameter.put("message", message);
parameter.put("page", page);
MessageDao messageDao = new MessageDao();
// 分页查询并返回结果
return messageDao.queryMessageListByPage(parameter);
}
这是service
/**
* 根据查询条件查询消息列表的条数
*/
public int count(Message message) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
int result = 0;
try {
sqlSession = dbAccess.getSqlSession();
// 通过sqlSession执行SQL语句
IMessage imessage = sqlSession.getMapper(IMessage.class);
result = imessage.count(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(sqlSession != null) {
sqlSession.close();
}
}
return result;
}
/**
* 根据查询条件分页查询消息列表
*/
public List<Message> queryMessageListByPage(Map<String,Object> parameter) {
DBAccess dbAccess = new DBAccess();
List<Message> messageList = new ArrayList<Message>();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
// 通过sqlSession执行SQL语句
IMessage imessage = sqlSession.getMapper(IMessage.class);
messageList = imessage.queryMessageListByPage(parameter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(sqlSession != null) {
sqlSession.close();
}
}
return messageList;
}
dao层的
@SuppressWarnings("serial") public class ListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置编码 req.setCharacterEncoding("UTF-8"); // 接受页面的值 String command = req.getParameter("command"); String description = req.getParameter("description"); String currentPage = req.getParameter("currentPage"); // 创建分页对象 Page page = new Page(); Pattern pattern = Pattern.compile("[0-9]{1,9}"); if(currentPage == null || !pattern.matcher(currentPage).matches()) { page.setCurrentPage(1); } else { page.setCurrentPage(Integer.valueOf(currentPage)); } QueryService listService = new QueryService(); // 查询消息列表并传给页面 req.setAttribute("messageList", listService.queryMessageListByPage(command, description,page)); // 向页面传值 req.setAttribute("command", command); req.setAttribute("description", description); req.setAttribute("page", page); // 向页面跳转 req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
这个是查询总条数的sql语句。
这个是jsp的page的语句。