2024年Java最新JavaWeb实现商品列表的多条件查询和分页功能(超详细的~),java面试问项目上线没有

最后

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

	}

	//查询第二个条件

	List<Goods> nameGoods = new ArrayList<Goods>();

	if (null != goodsName && !"".equals(goodsName)) {

		for (Goods goods2 : typeGoods) {

			if (goods2.getGoodsName().contains(goodsName)) {

				nameGoods.add(goods2);

			}

		}

	} else {

		nameGoods = typeGoods;

	}



**分页功能实现**



> 分页功能实现思路:  

> 1.输入条件,当前页,每页的数据记录条数,商品集合  

> 2.封装分页类,属性有:pageNumb:当前页,pageSize:每页的数据数,rowCount:总共的商品数据条数,pageCount:总共有多少页,prePageNumb:上一页,nextPageNumb:下一页,currentList:当前页数展示的商品集合,提供有参构造方法,构造Pager类



**Pager类**



public class Pager {

private Integer pageNumb;

private Integer pageSize;



private Integer rowCount;

private Integer pageCount;

private Integer prePageNumb;

private Integer nextPageNumb;

private List currentList;



public Pager(Integer _pageNumb, Integer _pageSize, List _allList){

	//初始化每页的内容条数

	this.pageSize = _pageSize;

	

	//初始化总共的内容条数

	this.rowCount = _allList.size();

	

	//计算共有多少页

	if (this.rowCount % this.pageSize == 0) {

		this.pageCount = this.rowCount / this.pageSize;

	} else {

		this.pageCount = this.rowCount / this.pageSize + 1;

	}

	

	//修正当前页

	if (_pageNumb <= 0) {

		this.pageNumb = 1;

	} else if (_pageNumb > this.pageCount) {

		this.pageNumb = this.pageCount;

	} else {

		this.pageNumb = _pageNumb;

	}

	

	//处理前一页和后一页的页码

	this.prePageNumb = this.pageNumb - 1;

	if (this.prePageNumb <= 0) {

		this.prePageNumb = 1;

	}

	this.nextPageNumb = this.pageNumb + 1;

	if (this.nextPageNumb > this.pageCount) {

		this.nextPageNumb = this.pageCount;

	}

	

	//处理当前页的记录开始结束位置

	Integer fromIndex = (this.pageNumb - 1) * 5;

	Integer toIndex = this.pageNumb * 5;

	if (toIndex > this.rowCount) {

		toIndex = this.rowCount;

	}

	this.currentList = _allList.subList(fromIndex, toIndex);

}



public Integer getPageCount() {

	return pageCount;

}



public Integer getPageNumb() {

	return pageNumb;

}



public Integer getPageSize() {

	return pageSize;

}



public Integer getRowCount() {

	return rowCount;

}



public Integer getPrePageNumb() {

	return prePageNumb;

}



public Integer getNextPageNumb() {

	return nextPageNumb;

}



public List getCurrentList() {

	return currentList;

}

}




**GoodsList类对其进行构造**



@WebServlet(“/goodsList”)

public class GoodsListServlet extends HttpServlet{

public static final Integer PageSize = 5;



@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	//获取查询条件

	String goodsType = req.getParameter("goods_type");

	String goodsName = req.getParameter("goods_name");

	//从文件读出商品集合

	List<Goods> goods = GoodsFileUtil.getGoodsList();

	//查询第一个条件

	List<Goods> typeGoods = new ArrayList<Goods>();

	if (null != goodsType && !"".equals(goodsType)) {

		for (Goods goods2 : goods) {

			if (goods2.getGoodsType().contains(goodsType)) {

				typeGoods.add(goods2);

			}

		}

	} else {

		typeGoods = goods;

	}

	//查询第二个条件

	List<Goods> nameGoods = new ArrayList<Goods>();

	if (null != goodsName && !"".equals(goodsName)) {

		for (Goods goods2 : typeGoods) {

			if (goods2.getGoodsName().contains(goodsName)) {

				nameGoods.add(goods2);

			}

		}

	} else {

		nameGoods = typeGoods;

	}

	//获取当前页

	String strPageNumb = req.getParameter("pageNumb");

	Integer pageNumb = 1;

	if (null != strPageNumb && !"".equals(strPageNumb)) {

		pageNumb = Integer.valueOf(strPageNumb);

	}

	for (int i = 1; i <= goods.size(); i++) {

		Goods good = goods.get(i-1);

		good.setGoodsNum(i);

	}

	//初始化pager对象

	Pager page = new Pager(pageNumb, PageSize, nameGoods);

	req.setAttribute("page", page);

	req.setAttribute("goodsType", goodsType);

	req.setAttribute("goodsName", goodsName);

	req.getRequestDispatcher("/WEB-INF/page1.jsp")

	.forward(req, resp);

}

}




**page.jsp页面**



<%@page import=“com.wanshi.bean.Pager”%>

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding="UTF-8"%>

最后

ActiveMQ消息中间件面试专题

  • 什么是ActiveMQ?
  • ActiveMQ服务器宕机怎么办?
  • 丢消息怎么办?
  • 持久化消息非常慢怎么办?
  • 消息的不均匀消费怎么办?
  • 死信队列怎么办?
  • ActiveMQ中的消息重发时间间隔和重发次数吗?

ActiveMQ消息中间件面试专题解析拓展:

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


redis面试专题及答案

  • 支持一致性哈希的客户端有哪些?
  • Redis与其他key-value存储有什么不同?
  • Redis的内存占用情况怎么样?
  • 都有哪些办法可以降低Redis的内存使用情况呢?
  • 查看Redis使用情况及状态信息用什么命令?
  • Redis的内存用完了会发生什么?
  • Redis是单线程的,如何提高多核CPU的利用率?

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


Spring面试专题及答案

  • 谈谈你对 Spring 的理解
  • Spring 有哪些优点?
  • Spring 中的设计模式
  • 怎样开启注解装配以及常用注解
  • 简单介绍下 Spring bean 的生命周期

Spring面试答案解析拓展

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


高并发多线程面试专题

  • 现在有线程 T1、T2 和 T3。你如何确保 T2 线程在 T1 之后执行,并且 T3 线程在 T2 之后执行?
  • Java 中新的 Lock 接口相对于同步代码块(synchronized block)有什么优势?如果让你实现一个高性能缓存,支持并发读取和单一写入,你如何保证数据完整性。
  • Java 中 wait 和 sleep 方法有什么区别?
  • 如何在 Java 中实现一个阻塞队列?
  • 如何在 Java 中编写代码解决生产者消费者问题?
  • 写一段死锁代码。你在 Java 中如何解决死锁?

高并发多线程面试解析与拓展

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


jvm面试专题与解析

  • JVM 由哪些部分组成?
  • JVM 内存划分?
  • Java 的内存模型?
  • 引用的分类?
  • GC什么时候开始?

JVM面试专题解析与拓展!

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 写一段死锁代码。你在 Java 中如何解决死锁?

高并发多线程面试解析与拓展

[外链图片转存中…(img-5lZbnAFH-1714912317066)]


jvm面试专题与解析

  • JVM 由哪些部分组成?
  • JVM 内存划分?
  • Java 的内存模型?
  • 引用的分类?
  • GC什么时候开始?

JVM面试专题解析与拓展!

[外链图片转存中…(img-MYWoFFc0-1714912317066)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将商品添加到商品展示列表,你需要进行以下步骤: 1. 创建一个商品类,包含商品的名称、描述、价格等属性。 2. 在数据库中创建一个商品表,包含与商品类相对应的字段。 3. 在网页中创建一个表单,允许用户输入商品信息。 4. 创建一个商品管理的 Servlet,处理用户提交的表单数据。 5. 在 Servlet 中将用户输入的商品信息插入到数据库中。 6. 在商品展示页面中读取数据库中的商品信息,并将其显示出来。 下面是一个简单的示例代码,仅供参考: 商品类: ```java public class Product { private int id; private String name; private String description; private double price; // 构造函数、Getter 和 Setter 方法省略 } ``` 商品管理 Servlet: ```java public class ProductServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 从表单中读取商品信息 String name = request.getParameter("name"); String description = request.getParameter("description"); double price = Double.parseDouble(request.getParameter("price")); // 将商品信息插入到数据库中 Connection conn = null; PreparedStatement stmt = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "password"); stmt = conn.prepareStatement("INSERT INTO product (name, description, price) VALUES (?, ?, ?)"); stmt.setString(1, name); stmt.setString(2, description); stmt.setDouble(3, price); stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } // 跳转到商品展示页面 response.sendRedirect("product.jsp"); } } ``` 商品展示页面: ```html <html> <head> <title>商品展示</title> </head> <body> <h1>商品列表</h1> <table> <thead> <tr> <th>名称</th> <th>描述</th> <th>价格</th> </tr> </thead> <tbody> <%! class Product { public int id; public String name; public String description; public double price; } %> <% List<Product> products = new ArrayList<>(); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "password"); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM product"); while (rs.next()) { Product product = new Product(); product.id = rs.getInt("id"); product.name = rs.getString("name"); product.description = rs.getString("description"); product.price = rs.getDouble("price"); products.add(product); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } for (Product product : products) { %> <tr> <td><%= product.name %></td> <td><%= product.description %></td> <td><%= product.price %></td> </tr> <% } %> </tbody> </table> <h2>添加商品</h2> <form action="ProductServlet" method="post"> <label>名称:</label> <input type="text" name="name"><br> <label>描述:</label> <input type="text" name="description"><br> <label>价格:</label> <input type="number" name="price"><br> <input type="submit" value="提交"> </form> </body> </html> ``` 需要注意的是,上述代码仅仅是一个示例,实际应用中还需要进行参数验证、异常处理等更加严谨的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值