servlet应用实现分类列表查询+首页新闻查询

本文详细介绍了如何在分页的基础上,通过分类查询新闻列表的方法,包括数据库查询、数据处理及前端展示等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
		}

	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值