赞
踩
业务逻辑五步走
A、浏览器如何发送请求:
浏览器的地址栏(get)
a标签(get)
form标签(get/post)
Ajax(get/post)
js的window.location.href=“url”(get)
B、请求是否需要参数
C、请求发送到jsp还是servlet
D、如果到servlet接下来是 请求转发还是URL重定向 响应ajax
E、请求转发/重定向 是到jsp还是servlet
一、浏览器访问商城首页
A、浏览器地址栏访问
B、无需传参
C、到servlet
D、请求转发
E、到index.jsp商城首页
@WebServlet("/home") public class HomeServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 无参 调用dao获取数据库所有商品 * */ IproductDAO productDAO = new ProductDAOImpl(); List<Map<String, Object>> allProduct = productDAO.listProducts(); /** * 共享请求参数 * */ req.setAttribute("allproduct",allProduct); /** * 请求转发到商城首页 * */ req.getRequestDispatcher("/views/index.jsp").forward(req,resp); } }
商品接口
public interface IproductDAO {
/**
* 查询所有商品展示到首页
* */
List<Map<String,Object>> listProducts();
实现类
public class ProductDAOImpl implements IproductDAO {
@Override
public List<Map<String, Object>> listProducts() {
/**
* 获取所有商品
* */
String sql="select * from product";
return DBUtil.executequery(sql);
}
商城首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"+"views/"; %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <link rel="icon" href="img/icon.png" type="image/x-icon"> <link rel="stylesheet" type="text/css" href="css/base-aa24246264.css"/> <link rel="stylesheet" type="text/css" href="css/welcome-fc9359d842.css"/> <link rel="stylesheet" type="text/css" href="css/sidebar.css"/> <link rel="stylesheet" type="text/css" href="css/content.css"/> <title>贵美·商城</title> </head> <body> <!-- 头部导航 --> <jsp:include page="/views/header.jsp"></jsp:include> <!--搜索框--> <div class="logo"></div> <div id="com-search"> <div class="search"> <div class="search-tab"> <span class="active">宝贝</span><span class="">店铺</span> </div> <div class="search-box"> <input class="search-txt" placeholder="" type="text"> <span class="search-btn"></span> <div class="suggest-box" style="display: none;"></div> </div> <div class="hotword"> <a target="_blank" href="#" style="color:#FF3366">连衣裙</a> <a target="_blank" href="#" style="color:#666666">运动鞋</a> <a target="_blank" href="#" style="color:#FF3366">雪纺衫</a> <a target="_blank" href="#" style="color:#FF3366">衬衫</a> <a target="_blank" href="#" style="color:#666666">薄外套</a> <a target="_blank" href="#" style="color:#666666">T恤</a> <a target="_blank" href="#" style="color:#666666">套装</a> <a target="_blank" href="#" style="color:#666666">牛仔裤</a> <a target="_blank" href="#" style="color:#FF3366">小白鞋</a> <a target="_blank" href="#" style="color:#666666">风衣</a> <a target="_blank" href="#" style="color:#FF3366">绑带凉鞋</a> <a target="_blank" href="#" style="color:#666666">粗跟单鞋</a> </div> </div> </div> <!--导航 --> <div id="nav"> <ul> <li> <a href="#" class="selected">首页</a> </li> <li> <a href="#">上衣</a> </li> <li> <a href="#">裙子</a> </li> <li> <a href="#">裤子</a> </li> <li> <a href="#">女鞋</a> </li> <li> <a href="#">包包</a> </li> <li> <a href="#">配饰</a> </li> <li> <a href="#">美妆</a> </li> </ul> </div> <hr /> <!--广告轮播图--> <div class="ad"> <div class="inner"> <img src="img/ad1.jpg"/> <img src="img/ad2.jpg"/> <img src="img/ad1.jpg"/> </div> </div> <!--商品展示--> <h2>贵美优选</h2> <div id="wrap" style="margin: auto"> <!-- 商品信息 --> <c:forEach var="p" items="${allproduct}"> <div class="products"> <!--商品图片--> <a href="/shop/product?pid=${p.pid}" class="pimg" style="background-image: url(${p.pimage});"></a> <div class="info"> <div class="part"> <!--商品价格--> <div class="price">¥${p.shopPrice}</div> <div class="collect"> <!--商品收藏--> <i class="icon-star"></i>${p.collect} </div> </div> <i class="icon-select"> <!--商品简介--> </i>商品简介 </div> </div> </c:forEach> </div> <!-- 底部导航 --> <jsp:include page="/views/footer.jsp"></jsp:include> </body> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。