欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 实战项目锤炼之仿天猫网上商城的实现流程

程序员文章站 2022-03-12 10:46:18
一、项目简述功能:前台:* 用户模块* 分类模块* 商品模块* 购物车模块* 订单模块后台:* 管理员模块* 分类管理模块* 商品管理模块* 订单模块二、项目运行环境配置: jdk1.8 + tomc...

一、项目简述

功能:

前台:

* 用户模块

* 分类模块

* 商品模块

* 购物车模块

* 订单模块

后台:

* 管理员模块

* 分类管理模块

* 商品管理模块

* 订单模块

二、项目运行

环境配置: jdk1.8 + tomcat8.5 + mysql + eclispe (intellij idea,eclispe,myeclispe,sts 都支持)

项目技术: jsp + c3p0+ servlert + html+ css + javascript + jquery + ajax + fileupload等等。

Java 实战项目锤炼之仿天猫网上商城的实现流程

Java 实战项目锤炼之仿天猫网上商城的实现流程

Java 实战项目锤炼之仿天猫网上商城的实现流程

Java 实战项目锤炼之仿天猫网上商城的实现流程

Java 实战项目锤炼之仿天猫网上商城的实现流程

基础信息操作代码:

基础信息操作:
 
public abstract class basebackservlet extends httpservlet {
 
    /* 添加 */
    public abstract string add(httpservletrequest request, httpservletresponse response, page page);
    /* 删除 */
    public abstract string delete(httpservletrequest request, httpservletresponse response, page page);
    /* 更新 */
    public abstract string update(httpservletrequest request, httpservletresponse response, page page);
    /* 修改 */
    public abstract string edit(httpservletrequest request, httpservletresponse response, page page);
    /* 显示 */
    public abstract string list(httpservletrequest request, httpservletresponse response, page page);
 
    protected categorydao categorydao = new categorydao();
    protected orderdao orderdao = new orderdao();
    protected orderitemdao orderitemdao = new orderitemdao();
    protected productdao productdao = new productdao();
    protected productimagedao productimagedao = new productimagedao();
    protected propertydao propertydao = new propertydao();
    protected propertyvaluedao propertyvaluedao = new propertyvaluedao();
    protected reviewdao reviewdao = new reviewdao();
    protected userdao userdao = new userdao();
 
    @override
    protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        try{
            /* 获取分页信息 */
            int start = 0;
            int count = 5;
            try{
                start = integer.parseint(req.getparameter("page.start"));
                count = integer.parseint(req.getparameter("page.count"));
            } catch(exception e) {
 
            }
 
            page page = new page(start, count);
 
            /* 借助反射,调用对应的方法 */
            string method = (string)req.getattribute("method");
            method m = this.getclass().getmethod(method, httpservletrequest.class, httpservletresponse.class, page.class);
            string redirect = m.invoke(this, req, resp, page).tostring();
 
            /* 根据方法的返回值,进行相应的客户端、服务端跳转,或仅输出字符串 */
            if(redirect.startswith("@")){
                resp.sendredirect(redirect.substring(1));
            } else if(redirect.startswith("%")){
                resp.getwriter().print(redirect.substring(1));
            } else {
                req.getrequestdispatcher(redirect).forward(req, resp);
            }
        } catch (nosuchmethodexception e) {
            e.printstacktrace();
        } catch (illegalaccessexception e) {
            e.printstacktrace();
        } catch (invocationtargetexception e) {
            e.printstacktrace();
        }
    }
 
    public inputstream parseupload(httpservletrequest request, map<string, string> params){
        inputstream is = null;
        try{
            diskfileitemfactory factory = new diskfileitemfactory();
            servletfileupload upload = new servletfileupload(factory);
            //设置上传的文件大小限制为10m
            factory.setsizethreshold(1024 * 1024);
 
            list items = upload.parserequest(request);
            iterator iter = items.iterator();
 
            while(iter.hasnext()){
                fileitem item = (fileitem) iter.next();
                if(!item.isformfield()){
                    //获取上传文件的输入流
                    is = item.getinputstream();
                } else {
                    string paramname = item.getfieldname();
                    string paramvalue = item.getstring();
                    paramvalue = new string(paramvalue.getbytes("iso-8859-1"), "utf-8");
                    params.put(paramname, paramvalue);
                }
            }
 
        } catch (fileuploadexception e) {
            e.printstacktrace();
        } catch (unsupportedencodingexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        }
        return is;
    }
 
}

用户信息操作代码:

用户信息操作:
 
public class userservlet extends basebackservlet {
 
    @override
    public string add(httpservletrequest request, httpservletresponse response, page page) {
        system.out.println("调用了 userservlet add方法");
        return null;
    }
 
    @override
    public string delete(httpservletrequest request, httpservletresponse response, page page) {
        system.out.println("调用了 userservlet delete方法");
 
        int id = integer.parseint(request.getparameter("id"));
        userdao.delete(id);
        return "@admin_user_list";
    }
 
    @override
    public string update(httpservletrequest request, httpservletresponse response, page page) {
        system.out.println("调用了 userservlet update方法");
 
        map<string, string> params = new hashmap<>();
        inputstream is = super.parseupload(request, params);
        system.out.println(params);
 
        int id = integer.parseint(params.get("id"));
        string password = params.get("password");
 
        user user = userdao.get(id);
        user.setpassword(password);
 
        userdao.update(user);
 
        return "@admin_user_list";
    }
 
    @override
    public string edit(httpservletrequest request, httpservletresponse response, page page) {
        system.out.println("调用了 userservlet edit方法");
 
        int id = integer.parseint(request.getparameter("id"));
        user u = userdao.get(id);
 
        request.setattribute("u", u);
 
        return "admin/edituser.jsp";
    }
 
    @override
    public string list(httpservletrequest request, httpservletresponse response, page page) {
        system.out.println("调用了 userservlet list方法");
 
        list<user> us = userdao.list(page.getstart(), page.getcount());
        int total = userdao.gettotal();
        page.settotal(total);
 
        request.setattribute("us", us);
        request.setattribute("page", page);
 
        return "admin/listuser.jsp";
    }
}

以上就是java 实战项目锤炼之仿天猫网上商城的实现流程的详细内容,更多关于java 天猫商城的资料请关注其它相关文章!