complete重新加载
在分页的基础上,通过分类查询新闻列表
数组的长度不可变
当你点·国内的时候 左下角会出来对应的地址
在dao层
/*分页查询首页的新闻列表 */
List<NewsDetail> getIndexListForPaging(Long categoryId,int startIndex,int pageSize)throws Exception;
/** 查询首页新闻的总记录数 总记录数决定着你的页码 */
long getIndexCount(Long categoryId)throws Exception;
在dao实现类层
@Override
public List<NewsDetail> getIndexListForPaging(Long categoryId, int startIndex, int pageSize) throws Exception {
List<NewsDetail> news = new ArrayList<>();
try {
/*
* String sql = " select " +
* " id,categoryId,title,summary,content,picPath,author,createDate,modifyDate "
* + " from news_detail " + " order by createDate DESC " + " limit ?,? ";
* Object[] params = { startIndex, pageSize };
*/
StringBuffer sql = new StringBuffer();
sql.append("select ");
sql.append(" id,categoryId,title,summary,content,picPath,author,createDate,modifyDate ");
sql.append(" from news_detail ");
List<Object> params=new ArrayList<>();
//结合数据库
if(categoryId!=null && categoryId>0) {
sql.append(" where categoryId=?");
params.add(categoryId);
}
sql.append(" order by createDate DESC ");
sql.append(" limit ?,?");
params.add(startIndex);
params.add(pageSize);
this.executeQuery(sql.toString(), params.toArray());
while (rs.next()) {
long id = rs.getLong("id");
long categoryId1 = rs.getLong("categoryId");
String title = rs.getString("title");
String summary = rs.getString("summary");
String content = rs.getString("content");
String picPath = rs.getString("picPath");
String author = rs.getString("author");
Date createDate = rs.getTimestamp("createDate");
Date modifyDate = rs.getTimestamp("modifyDate");
news.add(new NewsDetail(id, categoryId1, title, summary, content, picPath, author, createDate,
modifyDate));
}
} finally {
this.closeResource();
}
return news;
}
@Override
public long getIndexCount(Long categoryId) throws Exception {
long count = 0;
try {
String sql = "select count(1) from news_detail";
List<Object> params=new ArrayList<>();
//结合数据库
if(categoryId!=null && categoryId>0) {
sql+=(" where categoryId=?");
params.add(categoryId);
}
this.executeQuery(sql, params.toArray());
while (rs.next()) {
count = rs.getLong(1);
}
} finally {
this.closeResource();
}
return count;
}
在service层
void getIndexListForPaging(Long categoryId,PageBean<NewsDetail> pageBean)throws Exception;
在service层实现类中
@Override
public void getIndexListForPaging(Long categoryId,PageBean<NewsDetail> pageBean) throws Exception {
long totalCount = newsDetailDao.getIndexCount(categoryId);
pageBean.setTotalCount(totalCount);
// int startIndex = (pageBean.getCurrentPage() - 1) * pageBean.getPageSize();
List<NewsDetail> list = newsDetailDao.getIndexListForPaging(categoryId,pageBean.getStartIndex(), pageBean.getPageSize());
pageBean.setList(list);
}
controller层
// url和index.jsp页面的地址需对应 写成/news
@WebServlet(name = "newsServlet", urlPatterns = "/news")
public class NewsServlet extends HttpServlet {
private void doIndexNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
String p = request.getParameter("p");
int currentPage = (p != null) ? Integer.parseInt(p) : 1;
String cId = request.getParameter("categoryId");
Long categoryId=(cId==null)?0:Long.parseLong(cId);
PageBean<NewsDetail> pageBean = new PageBean<>();
pageBean.setCurrentPage(currentPage);
// 现在前端没有开放指定每页显示多少条
pageBean.setPageSize(Constants.DEFAULT_INDEX_NEWS_PAGESIZE);
newsDetailService.getIndexListForPaging(categoryId, pageBean);
// 查询所有的新闻分类列表
List<NewsCategory> categoryList = newsCategoryService.getList();
// 在一次请求中有效
request.setAttribute("pageBean", pageBean);
request.setAttribute("categoryList", categoryList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 在out上面设置 如果不写上面的一行 页面就会输出问号
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
// 创建对象的 统一写一次就可以
// 获取不同请求传递过来的opr(operation)判断当前是什么请求
String opr = request.getParameter("opr");
try {
if ("indexNews".equals(opr)) {// 新闻首页分页执行
doIndexNews(request, response);
} else if ("adminNews".equals(opr)) {// 管理员页面分页执行
doAdminNews(request, response);
} else if ("addNews".equals(opr)) {// 添加新闻
doAddNews(request, response);
} else if ("readNews".equals(opr)) {
doReadNews(request, response);
} else {
out.print("功能正在研发中");
}
} catch (Exception e) {
e.printStackTrace();
}
}