javaweb商城之后端(无框架)(二)界面,商品显示功能
2018-02-25 00:32
381 查看
1.界面显示,获取商品分类类别(后端)
数据库表category
访问主页,得到分类,
访问一个servlet,执行相应操作,发送数据到主页显示,
可以直接数据库访问,得到category库得到表内容,封装category类对象,setAttribute,接着foreach+${category.cname }
也可以ajax,
ajax<script type="text/javascript">
//index.jsp加载完毕后 去服务器端获得所有的category数据
$(function(){
var content = "";
$.post(
"${pageContext.request.contextPath}/categoryList",//发送至servlet
function(data){
//[{"cid":"xxx","cname":"xxxx"},{},{}],从servlet得到的data
//动态创建<li><a href="#">${category.cname }</a></li>
for(var i=0;i<data.length;i++){
content+="<li>"+data[i].cname+</li>";
}
//将拼接好的li放置到ul中
$("#categoryUl").html(content);
},
"json"
);
});
</script>servletpublic class CategoryListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}Category
public class Category { private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }servicepublic class ProductService {
public List<Category> findAllCategory() {
ProductDao dao = new ProductDao();
List<Category> categoryList = null;
try {
categoryList = dao.findAllCategory();
} catch (SQLException e) {
e.printStackTrace();
}
return categoryList;
}
}
daopublic class ProductDao {
public List<Category> findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
return runner.query(sql, new BeanListHandler<Category>(Category.class));
}
}大致这样,就能得到分类,然后点击分类,显示相应的商品,
2.显示某类型商品列表
通过分类的cid(product表product类一样有的)查找product所以需要在ajax得到分类时在分类超链接处顺便发送cid至查找产品servlet
/productListByCid?cid="+data[i].cid+"
select *显示产品按cid分类,
还有上一页下一页,domain包除了建Product类之外还需要建个页面类PageBean类
Product类对应数据库表,
PageBean类
private int currentPage;//当前页数
private int currentCount;//单页面个数,
private int totalCount;//该类型产品总个数
private int totalPage;//总页数,不同产品不同页数
private List<T> list;//产品信息List表示,T-Product
Servlet
public class ProductListByCidServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得cid
String cid = request.getParameter("cid");
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) currentPageStr="1";//刚点类别自动跳第一页
int currentPage = Integer.parseInt(currentPageStr);//转类型
int currentCount = 8;//每页8个商品
ProductService service = new ProductService();
PageBean pageBean = service.findProductListByCid(cid,currentPage,currentCount);//进行业务处理,发送cid,当前页数,页面个数
request.setAttribute("pageBean", pageBean);
request.setAttribute("cid", cid);
//定义一个记录历史商品信息的集合
List<Product> historyProductList = new ArrayList<Product>();
//获得客户端携带名字叫pids的cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
String pids = cookie.getValue();//3-2-1
String[] split = pids.split("-");
for(String pid : split){
Product pro = service.findProductByPid(pid);
historyProductList.add(pro);
}
}
}
}
//将历史记录的集合放到域中
request.setAttribute("historyProductList", historyProductList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
service
public class ProductService {public PageBean findProductListByCid(String cid,int currentPage,int currentCount) {ProductDao dao = new ProductDao();//封装一个PageBean 返回web-servlet层PageBean<Product> pageBean = new PageBean<Product>();//1、封装当前页pageBean.setCurrentPage(currentPage);//2、封装每页显示的个数pageBean.setCurrentCount(currentCount);//3、封装总条数,数据库查int totalCount = 0;try {totalCount = dao.getCount(cid);} catch (SQLException e) {e.printStackTrace();}pageBean.setTotalCount(totalCount);//4、封装总页数math.ceil-大于当前数的整数int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);pageBean.setTotalPage(totalPage);//5、当前页显示的数据-最重要的,显示的// select * from product where cid=? limit ?,?// 当前页与起始索引index的关系int index = (currentPage-1)*currentCount;List<Product> list = null;try {list = dao.findProductByPage(cid,index,currentCount);} catch (SQLException e) {e.printStackTrace();}pageBean.setList(list);return pageBean;daopublic int getCount(String cid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from product where cid=?";
Long query = (Long) runner.query(sql, new ScalarHandler(),cid);
return query.intValue();
}//根据cid查询该类型产品数量
public List<Product> findProductByPage(String cid, int index, int currentCount) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where cid=? limit ?,?";
List<Product> list = runner.query(sql, new BeanListHandler<Product>(Product.class), cid,index,currentCount);
return list;
}//根据cid,当前页的起始索引,每页个数查显示页显示的内容
view-jsp<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach items="${pageBean.list }" var="pro">
<div >
<img src="${pageContext.request.contextPath }/${pro.pimage}" >
</a>
<p>
${pro.pname }</a>
</p>
<p>
<font>商城价:¥${pro.shop_price }</font>
</p>
</div>
</c:forEach>
<!-- 上一页 -->
<c:if test="${pageBean.currentPage==1 }">
<li class="disabled">
<a href="javascript:void(0);">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1 }">
<li>
<a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}¤tPage=${pageBean.currentPage-1 }" >
<span>«</span>
</a>
</li>
</c:if>
<!-- 显示每一页 -->
<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<!-- 判断是否是当前页,是的话不能点击 -->
<c:if test="${page==pageBean.currentPage }">
<li class="active"><a href="javascript:void(0);">${page }</a></li>
</c:if>
<c:if test="${page!=pageBean.currentPage }">
<li><a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}¤tPage=${page }">${page }</a></li>
</c:if>
</c:forEach>
<!-- 下一页 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
<li class="disabled">
<a href="javascript:void(0);" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}¤tPage=${pageBean.currentPage+1 }" >
<span>»</span>
</a>
</li>
</c:if>
3.点击商品显示商品详情
点击查看商品,查看PageBean类list-Product类对象<a href="${pageContext.request.contextPath }/productInfo?pid=${pro.pid}}
servlet//获得要查询的商品的pid
String pid = request.getParameter("pid");
ProductService service = new ProductService();
Product product = service.findProductByPid(pid);
request.setAttribute("product", product);
request.getRequestDispatcher("/product_info.jsp").forward(request, response);
servicepublic Product findProductByPid(String pid) {
ProductDao dao = new ProductDao();
Product product = null;
try {
product = dao.findProductByPid(pid);
} catch (SQLException e) {
e.printStackTrace();
}
return product;
}daopublic Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
return runner.query(sql, new BeanHandler<Product>(Product.class), pid);
}view-jsp
${product.pname}
${product.。。。}
填充即可
相关文章推荐
- JavaWeb学习记录(十四)——商城购物之字符串拼接实现最近浏览商品和购物车的功能
- javaweb商城之后端(无框架)(一)注册功能
- 【JAVAWEB学习笔记】网上商城实战2:异步加载分类、Redis缓存分类和显示商品
- javaweb商城之后端(无框架)(三)浏览历史记录
- javaWeb购物商城设计---商品显示
- 手机商城第二天,各个功能按钮的切换与显示不同界面
- 互芯平台待机界面显示农历功能实现
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->Web版本模块管理界面新增模块排序功能
- 实现首页框架的tab签,点击模块在首页添加tab签之后,同时显示其对应的页面
- 第六天:浪迹天涯网上商城(1.0版本)--后台管理系统--实现商品类目选择功能
- iOS开发之旅(2):实现一个APP界面框架之搜索功能
- 360UI 界面框架 软件盘功能
- 【SSH网上商城项目实战08】查询和删除商品类别功能的实现
- libgdx游戏引擎开发笔记(九)SuperJumper游戏例子的讲解(篇三)---- 主游戏界面显示框架
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2-> “Tab”标签新增可“最大化”显示功能
- ubuntu进入登陆界面之后无法显示桌面
- Javaweb实现登录界面“记住我”功能
- 关于使用Android installer安装应用,安装好之后点击“打开”按钮进入应用之后的界面显示问题
- Datagridview中手动绑定列之后界面显示不一
- 商城项目实战24:实现添加商品功能