java代码
GoodsDao是与据库控制类
<%
GoodsDao dao =new GoodsDao();
String searchName = request.getParameter("searchName");//接收参数,用于搜索
ArrayList<Goods> goods = null;
if(searchName!=null){
goods = dao.getAllGoods(searchName);
}else{
goods = dao.getAllGoods();//如果没有参数则获取全部
}
String pageid=request.getParameter("page");//接收页码
int curpage=1;
if(pageid!=null){
curpage= Integer.parseInt(pageid);
}
int max_num=curpage*9;//根据页码确定指定范围的list对象
int totalpage = (int)Math.ceil((goods.size()/9.0)) ;//获取最大页,用于js中的判断
if(goods.size()>0){
for (int i = max_num-9; i < goods.size()&&i<max_num; i++) {
Goods gs = goods.get(i);
out.write("<div class='on'>");
out.write("<img src= '"+gs.getPic() +"' style=' margin-top:auto; margin-left:10px;' /><br />");
out.write("<br /> <a href='Untitled-1.jsp?id="+gs.getId() +"'>"+gs.getName() +"</a><br /> <br />");
out.write("<div class='onon'>¥"+gs.getPrice() +"</div>");
out.write("</div>");
}
}
%>
<div style="float: left;width: 100%; margin:50px; text-align: center;">
<a href="" id="pre">上一页</a>
<span><%=curpage%>/<%=totalpage%></span>
<a href="" id="next">下一页</a>
</div>
js代码
<script src="js/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function() {
$("#pre").click(function() {
var t_pre=<%=curpage%>;//获取当前页面页码
if(t_pre==1)
$(this).attr("href","#");
else{
var pre =t_pre-1;
$(this).attr("href","fenlei.jsp?page="+pre);//jQuery给属性设置值
}
});
$("#next").click(function() {
var max = <%=totalpage%>;
var cur=<%=curpage%>;
if(cur==max)
$(this).attr("href","#");
else{
var pre =cur+1;
$(this).attr("href","fenlei.jsp?page="+pre);
}
});
});
</script>