代理公司收费标准,深圳百度网站优化,前端开发是程序员吗,集团公司网站方案第一步分析我们做分页需要什么数据#xff1a;
我们从两个方向来分析#xff1a;1 页面方向#xff0c;2 servlet方向
一 #xff0c;页面方向
1 当前页 currPageCode
2 总 页数 totalPage
3一页中的记录数据 datas
二#xff0c;servlet方向
1 当前页 currPageCo…第一步分析我们做分页需要什么数据
我们从两个方向来分析1 页面方向2 servlet方向
一 页面方向
1 当前页 currPageCode
2 总 页数 totalPage
3一页中的记录数据 datas
二servlet方向
1 当前页 currPageCode
2 总页数 totalPage
3 每页记录数 pagesize
4 一共多少条记录 totalRecord
5 一页中的记录数据 datas
6当前页第一条记录的行数 currPageBeginIndex
7 url [html] view plain copy
public class PageBeanT { private int cp;//当前页 private int pc;//可以通过每页记录数和共多少条记录得到 dt%md 0 dt/md dt/md1 一共多少页 private int md;//每页记录数 private ListT pd;//页中的记录数据 private int dt;//一共多少条记录 private int cd;//可以通过每页记录是*(当前页数-1) 1得到当前页第一条记录的行数 private String url; //为了清楚起见我就没有粘上我的setget方法 }
嗯有了javabean了那摩就先让我们写一下servlet吧 [java] view plain copy
public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1 通过页面回传的当前页信息设置当前页 * 2 设置每页所显示的数据 * 3 调用servlce的findAll方法获得一共多少条记录及当前页的数据 * */ PageBeanCustomer pb new PageBeanCustomer(); if(request.getParameter(cp) ! null !request.getParameter(cp).trim().isEmpty()) { int cp Integer.parseInt(request.getParameter(cp)); pb.setCp(cp); } else { pb.setCp(1); } pb.setMd(10); pb.setPd(service.findAll(pb.getCd(),pb.getMd())); pb.setDt(service.count()); String url getUrl(request); pb.setUrl(url); request.setAttribute(pb,pb); return f:list.jsp; }
嗯有了数据了这个方法会转发找list.jsp页面中我们来看该页的技术点 [html] view plain copy
body h3 aligncenter客户列表/h3 table border1 width70% aligncenter tr th客户姓名/th th性别/th th生日/th th手机/th th邮箱/th th描述/th th操作/th /tr !-- 第几页 current page cp 共几页 page count pc 一页显示多少数据 many date md 每页所显示的数据 page date pd 一共多少条记录 date total dt 当前页的第一条记录的行数 current date cd -- !-- 1,这一点就不用多说了吧我们把我们查找的数据进行遍历 -- c:forEach varcustomer items${pb.pd} tr !-- private String cname; private String gender; private String birthday; private String cellphone; private String email; private String description; -- td${customer.cname }/td td${customer.gender }/td td${customer.birthday}/td td${customer.cellphone }/td td${customer.email }/td td${customer.description }/td td a hrefc:url value/customer2?methodforEditcid${customer.cid }/编辑/a a hrefc:url value/customer2?methoddeletecid${customer.cid }/删除/a /td /tr /c:forEach /table !-- 这是我们分页技术页面部分的实现; -- center 第${requestScope.pb.cp }页/共 ${pb.pc }页 a hrefc:url value${pb.url }/首页/a !-- 因为首页也是第一页所以我们没有加当前页码参数servlet会自动认为是第一页 -- c:if test${pb.cp 1 } !-- 如果当前页面是第一页德华那摩就禁用上一页 -- a hrefc:url value${pb.url }cp${pb.cp - 1 }/上一页/a /c:if %-- 用来进行中间页码的显示及使用超链接所带来的的问题的解决方案 如何显示页码呢我们其实只需要两个数据begin和end就哦了 我们常见的百度页面有什么特点呢你可以去看看我就不多说了下面我给出他的计算公式 如果总页数10列表长度那么begin1end总页数 否则 使用公式计算beginpc-5, endpc 4 两种特殊情况 头溢出当begin1时让begin1 尾溢出当end${tp}时让end${tp} --% c:choose c:when test${pb.pc 10 } !-- 判断总页数小于页面大小吗 -- c:set varbegin value1/ c:set varend value${pb.pc }/ /c:when c:otherwise c:set varbegin value${pb.cp-4 }/ c:set varend value${pb.cp5 }/ c:choose c:when test${begin 1 } c:set varbegin value1/ c:set varend value10/ /c:when c:when test${end pb.pc } c:set varbegin value${pb.pc-9 }/ c:set varend value${pb.pc }/ /c:when /c:choose /c:otherwise /c:choose !-- 通过上面的操作我们可以得到正确的begin和end了下面我们就对其进行遍历 -- c:forEach begin${begin } end${end } vari c:choose !-- 当前页面显示为普通文本 -- c:when test${i pb.cp }${i }/c:when !-- 其他页面显示为超链接 -- c:otherwisea hrefc:url value${pb.url }cp${i }/ [${i }] /a/c:otherwise /c:choose /c:forEach c:if test${pb.cp pb.pc } !-- 如果当前页面时最后一页的话我们就禁用下一页 -- a hrefc:url value${pb.url }cp${pb.cp 1 }/下一页/a /c:if a hrefc:url value${pb.url }cp${pb.pc }/尾页/a /center /body
哦了多余的话我就不解释了我想你看过这段代码之后会明白的吧。 那摩接下来让我们来写一个service和dao就算完事了service我就不说了我们直接来看dao吧 [java] view plain copy
public ListCustomer findAll(int index,int length) { String sql select * from t_customer limit ?,?; ListCustomer l null; Object[] p {index,length}; try { l queryRunner.query(sql, new BeanListHandlerCustomer(Customer.class),p); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return l; }
[java] view plain copy
public int count() { span stylewhite-space:pre; /spanString sql select count(*) from t_customer; span stylewhite-space:pre; /spanNumber n null; span stylewhite-space:pre; /spantry { span stylewhite-space:pre; /spann (Number)queryRunner.query(sql, new ScalarHandler()); span stylewhite-space:pre; /span span stylewhite-space:pre; /span} catch (SQLException e) { span stylewhite-space:pre; /span// TODO Auto-generated catch block span stylewhite-space:pre; /spane.printStackTrace(); span stylewhite-space:pre; /span} span stylewhite-space:pre; /spanreturn n.intValue(); span stylewhite-space:pre; /span} 是滴全部查找就是这摸简单只要还是得利用当前行数和每页记录数 哦了
有人会问我getUrl方法是什么鬼还有encoding方法又是你妈什么
应为我们是使用get提交的所以会造成乱码嗯对我们获取的customer对象他爹都不认识他我们的encoding方法就是用来进行解码的 [java] view plain copy
//这是其中的一个其他的属性也如同 if(c.getCname() ! null !c.getCname().trim().isEmpty()) { byte[] b null; try { b c.getCname().getBytes(iso-8859-1); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String namenull; try { name new String(b,utf-8); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } c.setCname(name); } getUrl是用来获取参数路径并去除cp参数的 [java] view plain copy
private String getUrl(HttpServletRequest request) { //String c request.getContextPath(); String q request.getQueryString(); String s request.getServletPath(); if(q ! null !q.trim().isEmpty()) { if( q.contains(cp)) { int i q.indexOf(cp); q q.substring(0, i); } } String url s ? q; return url; } 哦了大致的技术点我们就鸡巴说没了让我们提升点难度试试多条件查询 [java] view plain copy
//多条件查询的servlet方法 public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Customer customer new Customer(); try { BeanUtils.populate(customer,request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } Customer c encoding(customer); PageBeanCustomer pb new PageBeanCustomer(); if(request.getParameter(cp) ! null !request.getParameter(cp).trim().isEmpty()) { int cp Integer.parseInt(request.getParameter(cp)); pb.setCp(cp); } else { pb.setCp(1); } pb.setMd(10); pb.setPd(service.query(customer,pb.getCd(),pb.getMd())); pb.setDt(service.count()); String url getUrl(request); pb.setUrl(url); request.setAttribute(pb,pb); return f:list.jsp; }
多条件查询的dao [java] view plain copy
public ListCustomer query(Customer customer ,int index,int length) { //我们将其参数放入ArrayList中 ListObject l new ArrayList(); //我们使用StringBuffer连接我们所需要的条件 StringBuffer sql new StringBuffer (select * from t_customer where 11 ); if(customer.getCname() ! null !customer.getCname().trim().isEmpty()) { sql.append(and cname? ); l.add(customer.getCname()); } if(customer.getBirthday() ! null !customer.getBirthday().trim().isEmpty()) { sql.append(and birthday? ); l.add(customer.getBirthday()); } if(customer.getCellphone() ! null !customer.getCellphone().trim().isEmpty()) { sql.append(and cellphone? ); l.add(customer.getCellphone()); } if(customer.getDescription() ! null !customer.getDescription().trim().isEmpty()) { sql.append(and description? ); l.add(customer.getDescription()); } if(customer.getEmail() ! null !customer.getEmail().trim().isEmpty()) { sql.append(and email? ); l.add(customer.getEmail()); } if(customer.getGender() ! null !customer.getGender().trim().isEmpty()) { sql.append(and gender? ); l.add(customer.getGender()); } sql.append(limit ?,?); l.add(index); l.add(length); ListCustomer lc null; try { lc queryRunner.query(sql.toString(), new BeanListHandlerCustomer(Customer.class), l.toArray()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return lc; }