您的位置:首页 > 其它

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>

2. 这导航栏的是jsp的静态包含引入的。希望在所有的静态包含的 页面都可以动态的显示这个导航栏的内容。

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);

}

4. pageBean forEach

c+="<li><a href='product_list_servlet?cid='"+data[i].cid+">"+data[i].cname+"</a></li>";

上述错误需把两个单引号去掉。

5. 分页显示商品列表的显示易错点:pageBean的封装

当前页数,总页数,总条数,当前页显示内容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>

7. 浏览记录的显示:

就是在点击某个商品跳转到显示详情信息的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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐