17.12.19,web学习第二十九天,还有一年,努力吧青年ajax,cookie,分类分页显示
2017-12-20 10:22
351 查看
29.商城项目总结
1. 商城首页的显示我们不能要求客户去访问我们的servlet,所以我 们可以将欢迎页面覆盖掉(默认访问项目时访问index.jsp),在default.jsp嵌入java代码跳转到servlet再转发到商城首页。<% response.sendRedirect("HotProducts"); %> |
<welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> |
1)jsp页面嵌套java代码<% %>去数据库查询内容并显示到jsp页面。
2)在被引入的header.jsp的页面加载时使用ajax到服务器去数据库取数据再返回给jsp 页面。
<script> window.onload=function(){ $.post( "http://localhost:8080/sms/navigationBar", function(data){ var c=""; for (var i = 0; i < data.length; i++) { c+="<li><a href='#'>"+data[i].cname+"</a></li>"; } $("#navBar").html(c); }, "json" ) }; </script> 使用Gson转换: response.setContentType("text/html;charset=utf-8"); Gson gson = new Gson(); String json = gson.toJson(bar); response.getWriter().write(json);
3. 使用redis缓存导航栏的显示
1)导包+开启windows版的redis
2)使用jedis访问 JedisPoolConfig jpc = new JedisPoolConfig(); jpc.setMaxIdle(30); jpc.setMinIdle(5); jpc.setMaxTotal(50); JedisPool jp=new JedisPool(jpc, "localhost", 6379); Jedis jedis = jp.getResource(); String jedisjson = jedis.get("jedisjson"); if (jedisjson==null) { List<CategoryJB> bar = Service.navigationBar(); response.setContentType("text/html;charset=utf-8"); Gson gson = new Gson(); String json = gson.toJson(bar); response.getWriter().write(json); jedis.set("jedisjson", json); }else{ response.setContentType("text/html;charset=utf-8"); response.getWriter().write(jedisjson); } |
c+="<li><a href='product_list_servlet?cid='"+data[i].cid+">"+data[i].cname+"</a></li>"; 上述错误需把两个单引号去掉。 |
当前页数,总页数,总条数,当前页显示内容List,每页显示条 数。
Web层: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String parameter = request.getParameter("cid"); String pages = request.getParameter("cids"); if (pages==null) { pages="1"; } Integer of = Integer.valueOf(pages); System.out.println(of); pageBean<productJB> pb = Service.findProductListBycid(parameter,of); request.setAttribute("list", pb); request.setAttribute("cid", parameter); request.getRequestDispatcher("product_list.jsp").forward(request, response); } Service封装pageBean public static pageBean<productJB> findProductListBycid(String cid, Integer of) { pageBean<productJB> pb = new pageBean<productJB>(); //1当前页 int currentPage=of; pb.setCurrentPage(currentPage); //2每页显示的条数 int currentCount=12; pb.setCurrentCount(currentCount); //3.封装总条数 int i = Dao.findAllCategory(cid); pb.setTotalCount(i); //4封装总页数 int totalPage=(int) Math.ceil(1.0*i/currentCount); pb.setToatlpage(totalPage); //当前页显示的数据 int index=(currentPage-1)*currentCount; System.out.println("index"+index+"currentCount"+currentCount+"cid"+cid); List<productJB> ls = Dao.findProductByPage(cid,index,currentCount); pb.setList(ls); return pb; } Dao层查询: JSp页面显示: <c:forEach items="${list.list }" var="list"> <div class="col-md-2" style="height: 250px"> <a href="product_info.htm"> <img src="${list.pimage }" width="170" height="170" style="display: inline-block;"> </a> <p> <a href="product_info.html" style="color: green">${list.pname }</a> </p> <p> <font color="#FF0000">商城价:¥${list.shop_price }</font> </p> </div> </c:forEach> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <ul class="pagination" style="text-align: center; margin-top: 10px;"> <c:forEach begin="1" end="${list.toatlpage }" var="pages"> <ul class="pagination" style="text-align: center; margin-top: 10px;"> <li><a href="product_list_servlet?cids=${pages }&cid=${cid}">${pages }</a></li> </ul> </c:forEach> </ul> </div>
6. 易错拼href:
<li><a href="product_list_servlet?cids=${pages }">${pages }</a></li> |
就是在点击某个商品跳转到显示详情信息的servlet时,将这个商 品的pid传递到该servlet,该servlet拿到pid之后拼接cookie返
回给客户端。当客户端访问商品显示列表的servlet时,该servlet拿到cookie并遍历,将商品查询出来并添加到集合中,放到request域发给页面并在相应位置显示。
商品详细页面servlet: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String list = request.getParameter("list"); String cid = request.getParameter("cid"); String pid = request.getParameter("pid"); pids=pid; Cookie[] cookies = request.getCookies(); if (cookies!=null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("pids")) { pids = cookie.getValue(); String[] split = pids.split("-"); List<String> asList = Arrays.asList(split); LinkedList<String> linkedList = new LinkedList<String>(asList); if (linkedList.contains(pid)) { linkedList.remove(pid); linkedList.addFirst(pid); }else{ linkedList.addFirst(pid); } StringBuffer sb=new StringBuffer(); for (String string : linkedList) { sb.append(string); sb.append("-"); } pids = sb.substring(0, sb.length()-1); } } } Cookie pidd=new Cookie("pids", pids); response.addCookie(pidd); request.setAttribute("list", list); request.setAttribute("cid", cid); request.setAttribute("pid", pid); request.getRequestDispatcher("product_info.jsp").forward(request, response); } Product_info.jsp的跳转标签: <div> <a href="product_list_servlet?cid=${cid }">点击返回上一页</a> </div> product_list_servlet: Cookie[] cookies = request.getCookies(); if (cookies!=null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("pids")) { String value = cookie.getValue(); String[] split = value.split("-"); for (String string : split) { //System.out.println("数组String:"+string); productJB productJB = Service.findProbyPid(string); System.out.println("名字"+productJB.getPname()); findProbyPid.add(productJB); } } } } request.setAttribute("findProbyPid", findProbyPid);
相关文章推荐
- 17.12.01,web学习第十六天,还有一年,努力吧青年cookie和session
- 17.12.09,web学习第二十一天,还有一年,努力吧青年商品条件查询和分页查询
- 17.12.26,web学习第三十三天,还有一年,努力吧青年 商城项目第六天 ajax,文件上传
- 17.11.21,web学习第八天,还有一年,努力吧青年
- 18.01.06,web学习第三十八天,还有一年,努力吧青年 hibernate第四天 HQL,Criteria,离线对象,查询策略
- 17.12.17,web学习第二十八天,还有一年,努力吧青年shop
- 17.12.05,web学习第十九天,还有一年,努力吧青年事务mysql
- 17.12.25,web学习第三十二天,还有一年,努力吧青年 商城项目第五天 我的订单回显和文件上传
- 17.12.13,web学习第二十五天,还有一年,努力吧青年动态代理
- 18.01.11,web学习第四十天,还有一年,努力吧青年 struts2第二天 action创建+result处理+获取数据方式
- 17.12.16,web学习第二十七天,还有一年,努力吧青年redis
- 17.11.22,web学习第九天,还有一年,努力吧青年
- 17.11.13,web学习第三天,还有一年,努力吧青年!
- 17.12.14,web学习第二十六天,还有一年,努力吧青年 Linux入门
- 17.12.06,web学习第二十天,还有一年,努力吧青年商城增删改查记忆
- 17.12.12,web学习第二十四天,还有一年,努力吧青年过滤器Filter
- 17.12.04,web学习第十八天,还有一年,努力吧青年EL和JSTL
- 17.11.26,web学习第十二天,还有一年,努力吧青年HTTp协议和TomCat服务器
- 17.12.22,web学习第三十一天,还有一年,努力吧青年 商城项目第四天订单提交在线支付
- 17.11.15,web学习第四天,还有一年,努力吧青年!