一个不知名的项目--Day04
2017-08-20 23:16
169 查看
day04
后台开发–添加商品
表结构sevlset
Product product = VoUtils.ParseVo(Product.class, request.getParameterMap()); try { Part img = request.getPart("pimg"); String pprice = request.getParameter("pprice"); String pnum = request.getParameter("pnum"); ProdService instence = BasicFactory.factory.getInstence(ProdService.class); instence.checkedProduct(product, pprice, pnum); if (img.getSize()<=0) { throw new MsgException("图片没有上传"); } if (!img.getContentType().split("/")[0].equals("image")){ throw new MsgException("你上传的不是图片"); } product.setPimgurl(instence.saveImge(img,getServletContext().getRealPath(""))); instence.saveProduct(product); } catch (MsgException e) { e.printStackTrace(); request.setAttribute("msg", e.getMessage()); request.setAttribute("produt",product); request.getRequestDispatcher("/back/product_add.jsp").forward(request, response); return; } response.getWriter().write("<h1>添加商品成功<h1>"); response.setHeader("refresh","3;url="+request.getContextPath()+"back/product_add.jsp");
业务层
public String saveImge(Part img, String realPath) throws MsgException { StringBuilder path=new StringBuilder("/WEB-INF/productImg"); String str=Integer.toHexString( UUID.randomUUID().toString().hashCode()); for (int i=0;i<str.length();i++){ path.append("/").append(str.charAt(i)); } path.insert(0,realPath); File file=new File(path.toString()); file.mkdirs(); path.append("/").append(UUID.randomUUID().toString()).append(".") .append(img.getContentType().split("/")[1]); try { img.write(path.toString()); } catch (IOException e) { e.printStackTrace(); throw new MsgException("图片保存失败"); } return path.delete(0,realPath.length()).toString(); }
持久层
public void saveProduct(Product product) { String sql="insert into tb_product (pid,pname,pprice,pnum,pimgurl,pinfo,pcategory) " + " values(?,?,?,?,?,?,?)"; DaoUtils.upDate(sql,product.getPid(),product.getPname(),product.getPprice(), product.getPnum(),product.getPimgurl(), product.getPinfo(), product.getPcategory()); }
数据填充程序
{ String repath="C:\\Users\\honey\\Desktop\\xxxx\\web"; String[] cate={"手机数码","电脑平板","家用电器","汽车用品","食品饮料","图书杂志","服装服饰","理财产品"} ; for (int ii=0;ii<50;ii++) { BufferedImage bi =new BufferedImage(600,600,BufferedImage.TYPE_3BYTE_BGR); Graphics2D g2d = (Graphics2D) bi.getGraphics(); g2d.setColor(new Color(255,255,255)); g2d.fillRect(0,0,600,600); g2d.setFont(new Font("微软雅黑",Font.BOLD,50)); g2d.setColor(new Color(RandomVerifyUtils.RandomInt(0,234),RandomVerifyUtils.RandomInt(0,234),RandomVerifyUtils.RandomInt(0,234))); String name = "测试商品" + Integer.toHexString(UUID.randomUUID().toString().hashCode()); StringBuilder path = new StringBuilder("/WEB-INF/productImg"); String str1 = Integer.toHexString(UUID.randomUUID().toString().hashCode()); for (int i = 0; i < str1.length(); i++) { path.append("/").append(str1.charAt(i)); } path.insert(0, repath); File file = new File(path.toString()); file.mkdirs(); path.append(UUID.randomUUID().toString()).append(".jpg"); System.out.println("name = " + name); g2d.drawString(name, 50, 300); try { ImageIO.write(bi, "JPEG", new FileOutputStream(path.toString())); String sql = "insert into tb_product (pid,pname,pprice,pnum,pimgurl,pinfo,pcategory) " + " values(?,?,?,?,?,?,?)"; DaoUtils.upDate(sql, UUID.randomUUID().toString(), name, RandomVerifyUtils.RandomInt(0, 10000), RandomVerifyUtils.RandomInt(0, 100000), path.delete(0, repath.length()).toString(), Integer.toHexString(UUID.randomUUID().toString().hashCode()), cate[RandomVerifyUtils.RandomInt(0, cate.length - 1)]); } catch (IOException e) { e.printStackTrace(); } } }
jsp代码就不放
就放个图吧
商品展示
图片转发
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //以后可以装防盗链 String imgurl = request.getParameter("imgurl"); request.getRequestDispatcher(imgurl).forward(request, response); }
servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ProdService instence = BasicFactory.factory.getInstence(ProdService.class); String begin = request.getParameter("begin"); int now=0; if (!VoUtils.isNull(begin)){ now=Integer.parseInt(begin); } List<Product> list=instence.getProdList(now*15,15); int pagesum=instence.getpage(); if (pagesum>15){ pagesum/=15; }else{ pagesum=1; } if ((now+1)>pagesum){ now--; } request.setAttribute("prodlist",list); request.setAttribute("pagesum",pagesum); request.setAttribute("now",now); System.out.println("list.size() = " + list.size()); request.getRequestDispatcher("/back/product_list.jsp").forward(request, response); }
业务层
@Override public void saveProduct(Product product) { pd.saveProduct(product); } @Override public List<Product> getProdList(int begin,int end) { return pd.selectProduct(begin,end); } @Override public int getpage() { return pd.getCount(); }
持久层
//获取商品列表 public List<Product> selectProduct(int begin,int end) { String sql="select pid,pname,pprice,pnum,pimgurl,pinfo,pcategory from tb_product limit ?,?"; return DaoUtils.query(sql,new BeanListHandle<Product>(Product.class),begin,end); } @Override //获取数据条目总数 public int getCount() { String sql="SELECT count(*) from tb_product "; return DaoUtils.query(sql, new ResultSetHandle<Integer>() { @Override public Integer handle(ResultSet rs) throws SQLException { if (rs.next()){ return rs.getInt(1); } return -1; } });
总结
今天出去完了一下,回来被css,html卡了很久一脸头大,商品列表本来不想做分页的,那个页面有点问题,就做了简单的分页其他也没什么普通的业务逻辑
项目状态
相关文章推荐
- 一个不知名的项目--Day02
- 一个不知名的项目--Day03
- 一个不知名的项目--Day06
- 不知是香港的还是台湾的一个rtprtcp项目
- 一个不知名的项目--D 4000 ay05
- 一个不知名的项目---Day07
- 一个不知名的项目---Day01
- 一个项目分成两个项目来写时spring配置文件
- maven hello world: 使用maven前的一些配置和命令行创建一个webapp项目
- 关于【apache- tomcat- 5.5.15/conf /Catalina/localhost配置虚拟目录】时的一些问题。(配置web项目的方式不止一种,虚拟目录就是一个)
- android中如何将一个项目作为库项目
- MyEclipse工作集,将多个项目放到一个文件夹下
- springmvc+mybatis+maven项目集成的时候遇到一个问题,项目搭起来以后,http请求怎么都进不到controller中,页面直接返回404错误,控制台没有报任何错误,请大神帮我看看
- Sqlite的一个不知算不算缺陷的问题
- 一个下载Android开源项目源码的网站
- 一个项目重构方案设计
- day04-web项目
- 一个简单单片机项目的一些想法
- xcode创建一个游戏项目
- vue 如何新建一个项目