商城项目分解-首页加载"最新商品"信息

来源:互联网 发布:知乎 迪卡侬篮球鞋 编辑:程序博客网 时间:2024/05/16 14:24
首页{
    从数据库加载商品信息{
        功能概述:
            1.用户访问/index.jsp
            2./index.jsp将请求转发到IndexServlet先获取首页显示需要的数据
            3.IndexServlet获取数据并存到request域对象中,并将请求转发到真正的首页/jsp/index.jsp
            4./jsp/index.jsp从域对象中取出数据并显示
    }

}

/index.jsp核心代码

<jsp:forward page="/IndexServlet">    <jsp:param value="getIndexPage" name="method"/></jsp:forward>

IndexServlet核心代码

/** * 这是一个加载首页资源的类,前端页面先请求这边获取首页需要的数据,然后跳转至真正的首页 */@WebServlet("/IndexServlet")public class IndexServlet extends BaseServlet {    /**     * 获取首页需要用的资源(商品信息)     *     * @param request  请求     * @param response 响应     * @return 需要转发的页面URL     */    public String getIndexPage(HttpServletRequest request, HttpServletResponse response) {        try {            // 调用业务层获取最新商品的相关数据            // 这里使用的是BeanFactory工具通过反射获取Service层对象,用于解耦合            IndexServiceInter indexServiceInter = (IndexServiceInter) BeanFactory.getBean("IndexService");            List<Product> newProduce = indexServiceInter.getNewProduce();            // 将数据保存到request域对象中,并转发到首页进行显示            request.setAttribute("newProduce", newProduce);        } catch (SQLException e) {            e.printStackTrace();        }        // 这里是返回的跳转路径,有其父类的service方法接收并转发        return "/jsp/index.jsp";    }}
/jsp/index.jsp核心代码

<%--从域中取出最新商品的数据并遍历显示 start--%><c:forEach var="p" items="${ newProduce }">    <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">            <%--商品ID,点击超链接后跳转到商品详情页--%>        <a href="${ pageContext.request.contextPath }/ProductServlet?method=findByPid&pid=${ p.pid}">                <%--商品的图片--%>            <img src="${ pageContext.request.contextPath }/${ p.pimage}" width="130" height="130"                 style="display: inline-block;">        </a>                <%--商品名称--%>        <p><a href="${ pageContext.request.contextPath }/ProductServlet?method=findByPid&pid=${ p.pid}"              style='color:#666'>${ p.pname }</a></p>            <%--商品的售价--%>        <p><font color="#E4393C" style="font-size:16px">¥${ p.shop_price }</font></p>    </div></c:forEach><%--从域中取出最新商品的数据并遍历显示 end--%>


原创粉丝点击