分页查询关键代码:
通过servlet转发回来的各种信息进行分页的设计(转发回的信息有 分页查询的List集合 查询的页码 查询的条数 查询的数据库总条数 查询的总页码)
从开始时循环10次出现十个数字开始逐步细化
注意:jstl(java标砖标签库) 的判断条件 test="" 引号中的变量运算写在一个${ } EL表达式中,运算完再显示结果
<div align="center">
<c:if test="${pageNumber!=1 }">
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=1">第一页</a></span>
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber-1 }">上一页</a></span>
</c:if>
<!-- 分页查询的核心 -->
<c:forEach begin="1" end="${totalPages }" step="1" var="n">
<!-- n能显示出来的条件 -->
<c:if test="${(pageNumber-n)<=5&&(n-pageNumber)<=4 }">
<c:if test="${n!=pageNumber }">
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${n }">${n }</a></span>
</c:if>
<c:if test="${n==pageNumber }">
<span style="font-size:x-large;">${n }</span>
</c:if>
</c:if>
</c:forEach>
<c:if test="${pageNumber!=totalPages }">
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber+1 }">下一页</a></span>
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${totalPages }">最后一页</a></span>
</c:if>
</div>
多条件查询关键代码:
拼串的时候注意如果带?的不用写单引号'
如果不带?的sql串,记得带单引号'
public List<Product> queryMore(String queryName, String queryDesc) throws SQLException { String sql="SELECT * FROM product where 1=1 ";
if(queryName!=null && queryName.trim()!="") {
sql=sql+"and pname like '%"+queryName+"%'";
}
if(queryDesc!=null && queryDesc.trim()!="") {
sql=sql+" and pdesc like '%"+queryDesc+"%'";
}
System.out.println(sql);
List<Product> pros=qr.query(sql, new BeanListHandler<Product>(Product.class));
System.out.println(pros);
return pros;
}
删除选中商品关键代码:
public void deleteSelect(String[] checks) throws SQLException { String sql="delete from product where pid in(";
for(String str:checks) {
sql=sql+"'"+str+"'"+",";
}
sql=sql.substring(0, sql.length()-1);
sql=sql+ ")";
qr.update(sql);
}
前端部分
function deleteSelect(){
if(confirm("确认删除这些商品吗?")){
var form1=document.getElementById("form1");
form1.submit();
}
}
<c:if test="${not empty shangpins }">
<form method="post" action="${pageContext.request.contextPath }/shangpin?md=deleteSelect" id="form1">
<c:forEach items="${shangpins }" var="s">
<tr>
<td width="6%" align="center"><input name="checks" type="checkbox" value="${s.pid }"></td>
<td width="8%"><img width="180px" alt="" src="${s.pimage }"></td>
<td width="6%">${s.pname }</td>
<td width="8%">${s.shop_price }</td>
<td width="8%">${s.market_price }</td>
<td>${s.pdesc }</td>
<td width="8%"><a href="${pageContext.request.contextPath }/shangpin?md=huixian&pid=${s.pid}">修改</a></td>
<td width="8%"><a href="javascript:void(0);" onclick="agree('${s.pid}')">删除</a></td>
</tr>
</c:forEach>
</form>
</c:if>
修改要先回显再修改
<td width="8%"><a href="${pageContext.request.contextPath }/shangpin?md=huixian&pid=${s.pid}">修改</a></td>
private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
BeanUtils.populate(p, request.getParameterMap());
service.edit(p);
response.sendRedirect(request.getContextPath()+"/shangpin?md=findAll");
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("msg", "更新商品失败了");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
} } private void huixian(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
String pid=request.getParameter("pid");
Product product=service.getProduct(pid);
request.setAttribute("product", product);
request.getRequestDispatcher("/xiugaishangpin.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
request.setAttribute("msg", "回显商品失败了");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
}
}
同一业务集中使用同一servlet的方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String md=request.getParameter("md");
if("findAll".equals(md)) {
findAll(request,response);
}else if("add".equals(md)){
add(request,response);
}else if("huixian".equals(md)) {
huixian(request,response);
}else if("edit".equals(md)) {
edit(request,response);
}else if("delete".equals(md)) {
delete(request,response);
}else if("deleteSelect".equals(md)) {
deleteSelect(request,response);
}else if("queryMore".equals(md)){
queryMore(request,response);
}else if("findByPage".equals(md)) {
findByPage(request,response);
}
}
前端
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber-1 }">上一页</a></span>
用?md=findAll的方法
servlet判断md的值调用相应方法
也可以使用反射的方法代替