分页:limit ?,?
参数1 : startIndex 开始索引.
参数2 : pageSize 每页显示的个数
n 表示第几页 给定一个特殊的单词 pageNumber
select * from product; 第一页显示五条数据:
select * from product LIMIT 0 , 5;
第二页显示五条数据
select * from product LIMIT 5 , 5;
第三页显示五条数据
select * from product LIMIT 10 , 5; 第N页显示五条数据
select * from product LIMIT (pageNumber-1)*pageSize , pageSize; 如果只有一个参数 表示 第一页 显示几个数据
select * from product LIMIT 5; 简单分页查询步骤:
1.sql : select from table limit ?,?
2.startIntdex : 开始索引 不可以是负数
3.pageSize : 每页显示的个数
4.pageNumber : 用户访问的当前页
5.由页面发起传入 pageNumber 和 pageSize(目前写死) 在service层中进行计算startIndex
6.公式 (pageNumber-1)*pageSize 高级分页查询:
将service返回的六个参数,封装成一个PageBean
两个传的
pageNumber,pageSize.
两个查的
totalRecord,data
两个算的
startIndex,totalPage 在service编写代码逻辑 --> 将所有的代码逻辑封装到PageBean中 /* <!--分页 class="disabled" class="active"-->
<div style="width:1100px;margin:0 auto;margin-top:50px;"> <ul class="pagination" style="text-align:center; margin-top:10px;"> <%-- 如果当前页 等于 1 不需要再显示上一页 如果当前不等于1 显示 --%>
<c:if test="${pageBean.pageNumber != 1}">
<li ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=1" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
<li ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.pageNumber - 1 }" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
</c:if> <%-- 循环显示 所有的页数 --%>
<c:forEach begin="${pageBean.start}" end="${pageBean.end}" var="num" step="1">
<li ${pageBean.pageNumber == num ? " class='active'" : ""} ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${num}">${num}</a></li>
</c:forEach> <%-- 如果当前页小于总页数 显示下一页 --%>
<c:if test="${pageBean.pageNumber < pageBean.totalPage}">
<li><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.pageNumber + 1 }" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
<li><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.totalPage}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
</c:if> </ul> </div>
<!-- 分页结束======================= --> 以后专门用来处理分页的数据:
泛型的定义:
1:方法级别的定义,修饰和返回值之间<T>
2:类级别泛型定义,类名后<T>
public class PageBean<T> {
/*
一共六个参数:
2个传入:
pageNumber : 表示当前页
pageSize : 表示每页显示个数
2个查询
data : 分页的数据
totalRecord : 总记录数
2个计算
startIndex : 开始索引
totalPage : 总页数
private int pageNumber;
private int pageSize;
private int totalRecodr;
private int startIndex;
private int totalPage;
private List<T> data; public int getStartIndex() {
startIndex = (pageNumber - 1)*pageSize;
} public int getTotalPage() {
if(totalRecord % pageSize == 0) {
totalPage = totalRecord / pageSize;
}else {
totalPage = totalRecord / pageSize + 1;
}
return totalPage;
} public int getPageNumber() {
return pageNumber;
} public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
}
开发中真实案例--前五后四动态条 - 扩展 - 前四后五
开发中应用:
//循环的开始
private int start;
//循环结束
private int end; //计算 开始 和 结束的数据
private void jisuan() {
//总页数有没有可能小于10
//小于 10 不需要前四后五的动态条
if(getTotalPage() < 10) {//必须调用getTotalPage() 因为totalPage需要提前计算
start = 1;
end = totalPage;//最后一页等于最大页数 }else {
//总页数一定大于10 需要前四后五动态条
start = pageNumber - 4;
end = pageNumber + 5;
//如果start < 1 表示 当前1 2 3 4 总共显示10页
if(start < 1) {
start = 1;
end = 10;
} //如果end > totalPage 需要 计算start值
if(end > totalPage) {
end = totalPage;
start = totalPage - 9;
} } } 案例:代码体现 public class ProductServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
//获得页面传递的参数 用来执行某段代码
String method = request.getParameter("method"); ProductService pService = new ProductService();
CategoryService cService = new CategoryService();
//根据页面功能执行某段特殊的代码
if("findAll".equals(method)){
/**
* 1.类型问题 两种解决方式: 1.正则 2.try
* 2.索引不可以是负数
*/
//1.获得
// 获得分页的数据 pageNumber
String pageNumberStr = request.getParameter("pageNumber");
int pageNumber = 1;
try {
//如果是a 强转失败 赋值操作没有成功 pageNumber = 1
pageNumber = Integer.valueOf(pageNumberStr);
if(pageNumber < 1 ){
pageNumber = 1;
} } catch (Exception e) {
}
int pageSize = 2;
//2.处理
//List<Product> pList = pService.findByPage(pageNumber , pageSize);
PageBean<Product> pageBean = pService.findByPage(pageNumber , pageSize);
//3.响应
//3.1 将数据存到request
request.setAttribute("pageBean", pageBean);
//3.2 请求转发给jsp
request.getRequestDispatcher("/admin/product/product_list.jsp").forward(request, response);
return;
} //添加商品前查询分类的数据
if("addProductUI".equals(method)){
//1.获得
//2.处理
//2.1 调用service 查询出所有分类 返回值 List<Category> List<Category> cList = cService.findAll();
//3.响应
//3.1 将数据存到request
request.setAttribute("cList", cList);
//3.2 请求转发给product_add.jsp
request.getRequestDispatcher("/admin/product/product_add.jsp").forward(request, response); return;
} //添加商品数据
if("addProduct".equals(method)){ //1.获得
//1.1 获得map
Map<String, String[]> map = request.getParameterMap();
//1.2 创建Product
Product product = new Product();
//1.3 封装数据
BeanUtils.populate(product, map);
//1.4 手动封装数据
product.setPid( UUID.randomUUID().toString().replace("-", "") );//主键会随机生成
product.setPimage("products/1/c_0001.jpg");
product.setPdate(new Date().toLocaleString());
product.setIs_hot(0);//0表示非热门
product.setPflag(0);//0表示未下架
//2.处理
//调用service保存
pService.save(product);
//3.响应
//重定向到查询所有的案例
response.sendRedirect(request.getContextPath() +"/ProductServlet?method=findAll");
return; } //根据pid查询出商品的信息
if("findByPid".equals(method)){
//1.获得
//获得pid的编号
String pid = request.getParameter("pid");
//2 处理
//2.1 商品数据
Product product = pService.findByPid(pid);
//2.2 分类的数据
List<Category> cList = cService.findAll();
//3 响应
request.setAttribute("product", product);
request.setAttribute("cList", cList);
request.getRequestDispatcher("/admin/product/product_edit.jsp").forward(request, response);
return;
} //修改商品数据的代码
if("editProduct".equals(method)){
//1.获得
//1.1 获得map集合
Map<String, String[]> map = request.getParameterMap();
//1.2 创建product对象
Product product = new Product();
//1.3 封装
BeanUtils.populate(product, map);
//2.处理
//2.1 调用service修改
boolean flag = pService.update(product);
//3.响应
//3.1 重定向到查询所有的案例
if(flag){
//成功
response.sendRedirect(request.getContextPath() + "/ProductServlet?method=findAll");
}
return ;
} //根据pid删除商品
if("deleteByPid".equals(method)){
//1.获得
//获得pid
String pid = request.getParameter("pid");
//2.处理
int count = pService.deleteByPid(pid);
//3.响应
if(count > 0){
response.sendRedirect(request.getContextPath() + "/ProductServlet?method=findAll");
}
return;
} } catch (Exception e) {
e.printStackTrace();
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} public static void main(String[] args) {
System.out.println(UUID.randomUUID().toString().replace("-", "")); } } public class ProductService { /*public PageBean<Product> findByPage(int pageNumber, int pageSize) throws SQLException {
//创建一个即将被返回的对象 PageBean<Product>
PageBean<Product> pageBean = new PageBean<Product>(); ProductDao dao = new ProductDao(); //将pageNumber 转换成startIndex
int startIndex = (pageNumber - 1) * pageSize; //查询当前页的2条数据
List<Product> data = dao.findByPage( startIndex, pageSize); //查询总记录数
int totalRecord = dao.findRecord(); //计算总页数
int totalPage = 0;
if(totalRecord % pageSize == 0 ){
totalPage = totalRecord / pageSize;
}else{
totalPage = totalRecord / pageSize + 1;
} //赋值
pageBean.setData(data);
pageBean.setPageNumber(pageNumber);
pageBean.setPageSize(pageSize);
pageBean.setStartIndex(startIndex);
pageBean.setTotalPage(totalPage);
pageBean.setTotalRecord(totalRecord);
return pageBean;
}*/
public PageBean<Product> findByPage(int pageNumber, int pageSize) throws SQLException {
//创建一个即将被返回的对象 PageBean<Product>
PageBean<Product> pageBean = new PageBean<Product>( pageNumber , pageSize); ProductDao dao = new ProductDao(); //查询当前页的2条数据
List<Product> data = dao.findByPage( pageBean.getStartIndex() , pageSize); //查询总记录数
int totalRecord = dao.findRecord(); //赋值
pageBean.setData(data);
pageBean.setTotalRecord(totalRecord);
return pageBean;
} public List<Product> findAll() throws SQLException {
ProductDao dao = new ProductDao();
return dao.findAll();
} public void save(Product product) throws SQLException {
ProductDao dao = new ProductDao();
dao.save(product);
} public Product findByPid(String pid) throws SQLException {
ProductDao dao = new ProductDao();
return dao.findByPid(pid);
} public boolean update(Product product) throws SQLException {
ProductDao dao = new ProductDao();
return dao.update(product);
} public int deleteByPid(String pid) throws SQLException {
ProductDao dao = new ProductDao();
return dao.deleteByPid(pid);
} } package com.baidu.domain; public class Product { /*
`pid` VARCHAR(32) NOT NULL,
`pname` VARCHAR(50) DEFAULT NULL, #商品名称
`market_price` DOUBLE DEFAULT NULL, #商场价 `shop_price` DOUBLE DEFAULT NULL, #商城价
`pimage` VARCHAR(200) DEFAULT NULL, #商品图片路径
`pdate` DATE DEFAULT NULL, #上架时间 `is_hot` INT(11) DEFAULT NULL, #是否热门:0=不热门,1=热门
`pdesc` VARCHAR(255) DEFAULT NULL, #商品描述
`pflag` INT(11) DEFAULT 0, #商品标记:0=未下架(默认值),1=已经下架 `cid` VARCHAR(32) DEFAULT NULL, #分类id*/ private String pid;
private String pname;
private double market_price; private double shop_price;
private String pimage;
private String pdate; private int is_hot;
private String pdesc;
private int pflag; private String cid; public String getPid() {
return pid;
} public void setPid(String pid) {
this.pid = pid;
} public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public double getMarket_price() {
return market_price;
} public void setMarket_price(double market_price) {
this.market_price = market_price;
} public double getShop_price() {
return shop_price;
} public void setShop_price(double shop_price) {
this.shop_price = shop_price;
} public String getPimage() {
return pimage;
} public void setPimage(String pimage) {
this.pimage = pimage;
} public String getPdate() {
return pdate;
} public void setPdate(String pdate) {
this.pdate = pdate;
} public int getIs_hot() {
return is_hot;
} public void setIs_hot(int is_hot) {
this.is_hot = is_hot;
} public String getPdesc() {
return pdesc;
} public void setPdesc(String pdesc) {
this.pdesc = pdesc;
} public int getPflag() {
return pflag;
} public void setPflag(int pflag) {
this.pflag = pflag;
} public String getCid() {
return cid;
} public void setCid(String cid) {
this.cid = cid;
} } public class ProductDao {
/**
* 分页的dao
* @param startIndex
* @param pageSize
* @return
* @throws SQLException
*/
public List<Product> findByPage(int startIndex, int pageSize) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql =" select * from product limit ?,? ";
Object [] params = {
startIndex , pageSize
};
return queryRunner.query(sql, new BeanListHandler<Product>(Product.class), params);
}
/**
* 计算总记录数
* @return
* @throws SQLException
*/
public int findRecord() throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql =" select count(*) from product ";
Object [] params = {
};
Object object = queryRunner.query(sql, new ScalarHandler(), params);
return Integer.valueOf(object.toString());
} public List<Product> findAll() throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql =" select * from product order by pdate desc "; Object [] params = {}; return queryRunner.query(sql, new BeanListHandler<Product>(Product.class), params);
} public void save(Product product) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql =" insert into product values(?,?,?,?,?,?,?,?,?,?) "; Object [] params = {
product.getPid() , product.getPname() , product.getMarket_price() ,
product.getShop_price() , product.getPimage() , product.getPdate() ,
product.getIs_hot() , product.getPdesc() , product.getPflag() ,
product.getCid()
}; queryRunner.update(sql, params);
} public Product findByPid(String pid) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql =" select * from product where pid = ? "; Object [] params = {pid}; return queryRunner.query(sql, new BeanHandler<Product>(Product.class), params);
} public boolean update(Product product) throws SQLException { QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = " update product set pname = ? , shop_price = ? , pdesc = ? , cid = ? where pid = ? "; Object [] params = {
product.getPname() , product.getShop_price() , product.getPdesc() ,
product.getCid() , product.getPid()
}; return queryRunner.update(sql, params) > 0 ;
} public int deleteByPid(String pid) throws SQLException { QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = " delete from product where pid = ? "; Object [] params = {
pid
}; return queryRunner.update(sql, params) ;
} /**
* 处理乱码的工具类
*
*/
public class EncodingFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
try { //处理post乱码
request.setCharacterEncoding("utf-8"); //处理响应
response.setHeader("content-type", "text/html;charset=utf-8"); //处理get乱码
HttpServletRequest myRequest = (HttpServletRequest)Proxy.newProxyInstance(
EncodingFilter.class.getClassLoader(),
request.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //可以对getParameter进行增强 get提交方式 //1.判断是get请求方式
String requestMethod= request.getMethod();
if("GET".equalsIgnoreCase(requestMethod)){
//get提交 只对getParameter方法进行拦截
String methodName = method.getName();
if("getParameter".equals(methodName)){
//get方式 并且 调用的getParameter方法
//获得以前乱码 return 不乱码
String tempValue = request.getParameter(args[0].toString());
//如果没有获得数据 防止空指针
if(tempValue == null){
return null;
}
return new String(tempValue.getBytes("iso-8859-1"),"utf-8"); }
} //不需要增强
return method.invoke(request, args);
}
}); //放行
chain.doFilter(myRequest, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
} } jsp代码:
<%-- 获得数据 并遍历 --%>
<c:forEach items="${pageBean.data}" var="product">
<tr onmouseover="this.style.backgroundColor = '#F5FAFE'"
onmouseout="this.style.backgroundColor = '#fff';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
<input type="checkbox" name="" value="" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
<img src="${pageContext.request.contextPath}/${product.pimage}" style="height: 200px;width:150px" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.cid}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.pname}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.shop_price}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.pdesc}
</td>
<td align="center" style="HEIGHT: 22px">
<%--编辑 --%>
<a href="${pageContext.request.contextPath}/ProductServlet?pid=${product.pid}&method=findByPid">
<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<%--查询 --%>
<a href="../user/view.html?userID=15">
<img src="${pageContext.request.contextPath}/images/button_view.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<%--删除 ctrl + q --%>
<a href="${pageContext.request.contextPath}/ProductServlet?method=deleteByPid&pid=${product.pid}">
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a> <%--提示删除 --%>
<a href="javascript:void(0)" onclick="deleteByPid('${product.pid}')">
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td> </tr>
</c:forEach>
<%-- 获得数据 并遍历 --%>
<script>
//javascript 需要在写在 header标签中
function deleteByPid(pid){
//alert(pid);
//提示用户
var flag = confirm("您确定要删除信息吗?");
if(flag){
//确定删除
//访问服务器删除的Servlet
//修改地址栏 跳转路径 等效我们自己点击超链接
location.href="${pageContext.request.contextPath}/ProductServlet?method=deleteByPid&pid="+pid;
}
}
</script>
</table>
</td>
</tr>
</TBODY>
</table>
<!--分页 class="disabled" class="active"-->
<div style="width:1100px;margin:0 auto;margin-top:50px;"> <ul class="pagination" style="text-align:center; margin-top:10px;"> <%-- 如果当前页 等于 1 不需要再显示上一页 如果当前不等于1 显示 --%>
<c:if test="${pageBean.pageNumber != 1}">
<li ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=1" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
<li ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.pageNumber - 1 }" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
</c:if> <%-- 循环显示 所有的页数 --%>
<c:forEach begin="${pageBean.start}" end="${pageBean.end}" var="num" step="1">
<li ${pageBean.pageNumber == num ? " class='active'" : ""} ><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${num}">${num}</a></li>
</c:forEach> <%-- 如果当前页小于总页数 显示下一页 --%>
<c:if test="${pageBean.pageNumber < pageBean.totalPage}">
<li><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.pageNumber + 1 }" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
<li><a href="${pageContext.request.contextPath}/ProductServlet?method=findAll&pageNumber=${pageBean.totalPage}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
</c:if> </ul> </div>
<!-- 分页结束======================= --> 条件查询:
1: 先在首页上查询分类数据,并且遍历显示
分析:
select * from product where cid = ? and pname = ?;
含有的可能性:
1.两者都有.
2.两者都没有.
3.有cid但没有pname
4.没有cid但有pname select count(*) from product where 1=1;
伪代码:
SQL = " select * from product where 1=1 ";
if(cid != null) {
SQL += " and cid = ? "
}
if(pname != null) {
SQL += " and pname like ? "
}
1: 修改表单 action属性,method属性,name属性,自定义的method判断逻辑.
2: 调用Servlet
a : 获得 cid和pname
b : 处理 调用service 调用dao(重要)
c : 响应 - 将数据存到request里,请求转发给product_list.jsp
3.对jsp页面进行改造 条件查询注意事项:
1 . Dao的sql部分,拼接的问题,使用 where 1=1 解决了脏读、不可重复读、
2 . Dao的参数拼接部分,使用List接收数据,最后将list转换成数组,解决.
3 . 查询完以后将数据返回给页面,但还需要加上,用户传入后台的cid和pname;
切记:条件查询 不可以 和 分页 同一个项目 但以后 条件查询需要和分页结合
4 . 如果条件查询和分页需要做到同一个项目,需要处理参数问题.
5 . PageBean 查询总记录数 --> 如果加入条件查询,查询总记录数时需要加上条件. 案例:代码体现
public class ProductServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
//获得页面传递的参数 用来执行某段代码
String method = request.getParameter("method"); ProductService pService = new ProductService();
CategoryService cService = new CategoryService();
//根据页面功能执行某段特殊的代码
if("findAll".equals(method)){
//执行查询所有的代码
//1.获得
//1.31 获得cid
String cid = request.getParameter("cid");
//1.2 获得pname
String pname = request.getParameter("pname"); //2.处理
List<Product> pList = pService.findAll( cid , pname );
List<Category> cList = cService.findAll();
//3.响应
//3.1 将数据存到request
request.setAttribute("pList", pList);
request.setAttribute("cList", cList); request.setAttribute("cid", cid);
request.setAttribute("pname", pname); //3.2 请求转发给jsp
request.getRequestDispatcher("/admin/product/product_list.jsp").forward(request, response);
return;
} public class ProductDao {
/**
* 条件查询
* @param cid
* @param pname
* @return
* @throws SQLException
*/
public List<Product> findAll(String cid, String pname) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = " select * from product where 1=1 "; //定义一个List 作用 用来保存参数
List<Object> paramList = new ArrayList<Object>();
//cid判断
if(cid != null && !"".equals(cid)){
sql += " and cid = ? ";
paramList.add(cid);
}
//pname判断
if(pname != null && !"".equals(pname)){
sql += " and pname like ? ";
paramList.add("%"+pname+"%");
} //数组的缺陷 长度固定 集合的长度不固定 集合最后转换成数组
Object [] params = paramList.toArray(); return queryRunner.query(sql,new BeanListHandler<Product>(Product.class), params); } <form action="${pageContext.request.contextPath}/ProductServlet" method="post">
<%--隐藏域 --%>
<input type="hidden" name="method" value="findAll"/> <table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01" style="width: 15%">
分类
</td>
<td class="ta_01" bgColor="#ffffff" style="width: 30%">
<select name="cid" class="form-control">
<option value="">请选择</option>
<c:forEach items="${cList}" var="category">
<option ${category.cid==cid ? "selected" : ""} value="${category.cid}">${category.cname}</option>
</c:forEach>
</select>
</td>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01" style="width: 15%">
商品名称:
</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="pname" size="15" value="${pname}" class="form-control"/>
</td> 批量删除:
思想:
前台两套方法:
1 . 表单提交:将所有被选中的数据,的pid传入后台,后台执行批量删除.
2 . 修改sql 修改成 sql=" delete from product where pid in (?) " 数据循环拼接 1,2,4
3 . 开发中会使用jdbc jdbc当中有批处理,也可以处理批量任务. 事务特性:
A : 原子性;
C : 一致性;
I : 隔离性 : isolation 隔离会产生隔离问题
D : 持久性. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
//获得页面传递的参数 用来执行某段代码
String method = request.getParameter("method"); ProductService pService = new ProductService();
CategoryService cService = new CategoryService();
//根据pid删除商品
if("deleteByPid".equals(method)){
//1.获得
//获得数组集合
String[] pids = request.getParameterValues("pid");
//2.处理
int count = pService.deleteByPid(pids);
//3.响应
if(count > 0){
response.sendRedirect(request.getContextPath() + "/ProductServlet?method=findAll");
}
return;
} <td align="center" width="6%">
<input type="button" value="删除选中" onclick="deleteAll()">
<script>
function deleteAll(){
//1.提示
var flag = confirm("您确定要删除信息吗?");
if(!flag){
return ;
}
//2.获得所有选中的pid 获得所有的数组 最后的格式 pid=1&pid=2&pid3..
var pids = $("input[name='pid']:checked").serialize() ;
//3.发送
location.href="${pageContext.request.contextPath}/ProductServlet?method=deleteByPid&"+pids;
}
</script>
</td>
<td align="center" width="15%">
预览图
</td>
<td align="center" width="12%">
商品分类
</td>
<td align="center" width="25%">
商品名称
</td>
<td align="center" width="8%">
商品价格
</td>
<td width="11%" align="center">
描述
</td>
<td width="7%" align="center">
编辑
</td>
<td width="7%" align="center">
查看
</td>
<td width="7%" align="center">
删除
</td>
</tr>
<%-- 获得数据 并遍历 --%>
<c:forEach items="${pList}" var="product">
<tr onmouseover="this.style.backgroundColor = '#F5FAFE'"
onmouseout="this.style.backgroundColor = '#fff';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
<input type="checkbox" name="pid" value="${product.pid}" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
<img src="${pageContext.request.contextPath}/${product.pimage}" style="height: 200px;width:150px" />
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.cid}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.pname}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.shop_price}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">
${product.pdesc}
</td>
<td align="center" style="HEIGHT: 22px">
<%--编辑 --%>
<a href="${pageContext.request.contextPath}/ProductServlet?pid=${product.pid}&method=findByPid">
<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<%--查询 --%>
<a href="../user/view.html?userID=15">
<img src="${pageContext.request.contextPath}/images/button_view.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<%--删除 ctrl + q --%>
<a href="${pageContext.request.contextPath}/ProductServlet?method=deleteByPid&pid=${product.pid}">
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a> <%--提示删除 --%>
<a href="javascript:void(0)" onclick="deleteByPid('${product.pid}')">
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td> </tr>
</c:forEach>
<%-- 获得数据 并遍历 --%>
<script>
//javascript 需要在写在 header标签中
function deleteByPid(pid){
//alert(pid);
//提示用户
var flag = confirm("您确定要删除信息吗?");
if(flag){
//确定删除
//访问服务器删除的Servlet
//修改地址栏 跳转路径 等效我们自己点击超链接
location.href="${pageContext.request.contextPath}/ProductServlet?method=deleteByPid&pid="+pid;
}
}
</script>
</table>
</td>
</tr>
</TBODJY>
</table>

javaWeb核心技术之分页和条件的更多相关文章

  1. javaWeb核心技术第十二篇之分页和条件

    分页:limit ?,? 参数1 : startIndex 开始索引. 参数2 : pageSize 每页显示的个数 n 表示第几页 给定一个特殊的单词 pageNumber select * fro ...

  2. EF:分页查询 + 条件查询 + 排序

    /// <summary> /// linq扩展类---zxh /// </summary> /// <typeparam name="T">& ...

  3. jqGrid jqGrid分页参数+条件查询

    HTML <div class="row"> <div class="col-sm-20"> <form id="for ...

  4. angularjs+ionic的app端分页和条件

    做app项目积分商城的商品列表需要分页显示 实现: ionic滚动条:ion-scroll 用于创建一个可滚动的容器. 附:菜鸟教程:http://www.runoob.com/ionic/ionic ...

  5. springboot带分页的条件查询

    QueryDSL简介 QueryDSL仅仅是一个通用的查询框架,专注于通过Java API构建类型安全的SQL查询. Querydsl可以通过一组通用的查询API为用户构建出适合不同类型ORM框架或者 ...

  6. php分页的条件

    我们在项目开发的过程中避免不了使用分页功能,拿php来说,现在市面上有很多大大小小的php框架,当然了分页这种小功能这些框架中都是拿来直接可以用的. 这些框架的分页功能使用都很方便,配置一下分页所需参 ...

  7. 【JPA】Spring Data JPA 实现分页和条件查询

    文章目录 1.在`Repository`层继承两个接口 2.在Service层进行查询操作 3.Page的方法 1.在Repository层继承两个接口 JpaRepository<Admin, ...

  8. ajxa分页+多条件查询

    操作日志数据库表: 主页面: <script src="../fzl/jquery-1.11.2.min.js"></script> <script ...

  9. element-ui中用el-dialog+el-table+el-pagination实现文件默认选中且在分页的条件下有记忆功能

    需求: 点击按钮,出现列表弹框,选择需要的选项确认后显示选中的选项.每次点击按钮,列表中默认显示之前选中的文件. el-table-column,设type属性为selection,实现表格中多行选择 ...

随机推荐

  1. 541-反转字符串 II

    541-反转字符串 II 给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等 ...

  2. [Python]BeautifulSoup标签的遍历

    1.下行遍历 标签树的下行遍历.content 子节点列表,将tag所有儿子节点存入列表.children 子节点的迭代类型,与.contents类似用于循环遍历儿子节点.descendants 子孙 ...

  3. Real-time Compressive Tracking

    这是RTC算法的文献blog Real-time Compressive Tracking Kaihua Zhang1, Lei Zhang1, Ming-Hsuan Yang2 1Dept. of ...

  4. MariaDB Windows 安装

    1.复制安装文件到服务器 2.解压到指定的目录,并创建my.ini: 3.编辑my.ini文件内容 [client] port=3307 [mysql] default-character-set=u ...

  5. Git下载GitHub仓库里的某一个文件夹或某一个文件

    从Github上下载github上的整个项目,可以用下面指令: git clone https://github.com/XXX/xxxxx.git 其中:XXX是用户在Github上的用户名 xxx ...

  6. Bootstrap Table Api手册

    总是忘记在哪里,找起来又十分的费劲,特地记录下该手册地址. http://www.itxst.com/Bootstrap-Table/QuickStart.html

  7. JS高级---沙箱

    沙箱 沙箱: 环境, 黑盒, 在一个虚拟的环境中模拟真实世界, 做实验, 实验结果和真实世界的结果是一样, 但是不会影响真实世界     全局变量 var num=10; console.log(nu ...

  8. 10.3lambda表达式笔记

    与参数不同被捕获的变量的值是在lambda创建时拷贝,而不是调用时拷贝 void fcn() { int v1 = 42; //局部变量 auto f = [v1] { return v1; }; a ...

  9. mysql cmd链接不上数据库情况汇总

    在我的电脑 属性 高级设置 环境变量 path 编辑 添加mysql bin的文件位置复制粘贴上 mysql> use mysqlERROR 1044 (42000): Access denie ...

  10. monkey log 处理

    Monkey结果输出 1.保存在pc中 adb shell monkey [option] <count> >d:\monkey.txt 2.保存在手机中 adb shell mon ...