淘淘商城——购物车实现分析及工程搭建

关于购物车模块,京东和淘宝并不一样,京东允许用户在没有登录的情况下就使用购物车,而且加到购物车里面的商品可以一直保存着(其实是放到了Cookie当中,如果清空了Cookie也就清空购物车了)。而淘宝则是必须先登录才能将商品添加到购物车当中,就用户体验来说,京东的购物车模块用户体验更好。
我们看下京东购物车,如下图所示,可以看到域名是cart.jd.com,这与商品详情页面的item.jd.com是不一样的,说明京东的购物车模块是一个独立的工程。
淘淘商城——购物车实现分析及工程搭建_第1张图片
我们搭建购物车工程只需要搭建表现层工程就可以了,不需要服务端,这是因为购物车里面的商品信息,我们可以通过taotao-manager服务来获取。
现在我们就来新建一个taotao-cart-web工程,该工程可参考taotao-sso-web工程来搭建哟!
首先点击【File】菜单选项,并在下拉框中选中【New】,接着点击【Other】,如下:
淘淘商城——购物车实现分析及工程搭建_第2张图片
在输入框中输入maven,并选择Maven Project,如下:
淘淘商城——购物车实现分析及工程搭建_第3张图片
点击【Next】,勾选Create a simple project复选框,如果你不打上这个勾,它会让你选择一个骨架,但骨架里面是没有pom这个模板的。
淘淘商城——购物车实现分析及工程搭建_第4张图片
点击【Next】,出现如下对话框,在该对话框中定义maven工程的坐标,如下:
淘淘商城——购物车实现分析及工程搭建_第5张图片
注意:taotao-cart-web工程的打包方式是war,且须依赖父工程。
最后点击【Finish】,taotao-cart-web工程就创建好了,但是新建的web工程由于缺少web.xml文件而报错,解决方法是在webapp目录下新建一个WEB-INF目录,并在该目录下新建web.xml文件,至于该文件的内容具体是什么,后面会具体给出,这里我们并不着急。
接着配置taotao-cart-web工程的pom.xml文件,我们可参考taotao-sso-web工程的pom.xml文件来配置,只需稍作修改,将依赖的interface修改为taotao-manager-interface(第二个),最下面的tomcat插件端口号配置为8089,修改好的依赖如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.taotaogroupId>
        <artifactId>taotao-parentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <groupId>com.taotaogroupId>
    <artifactId>taotao-cart-webartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <dependencies>
        
        <dependency>  
            <groupId>com.taotaogroupId>  
            <artifactId>taotao-commonartifactId>  
            <version>0.0.1-SNAPSHOTversion>  
        dependency>
        
        <dependency>  
            <groupId>com.taotaogroupId>  
            <artifactId>taotao-manager-interfaceartifactId>  
            <version>0.0.1-SNAPSHOTversion>  
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
        dependency>
        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>springartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.jboss.nettygroupId>
                    <artifactId>nettyartifactId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
        dependency>
        <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
    dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <configuration>
                    <port>8089port>
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>
project>

紧接着来配置资源文件,我们亦可参考taotao-sso-web工程,将src/main/resources目录下的两个文件夹拷贝过来。先看resource目录下的resource.properties文件,该文件是用来配置常量的,目前我们还没有写业务代码,该文件暂时保持为空。
淘淘商城——购物车实现分析及工程搭建_第6张图片
下面再看下spring目录下的springmvc.xml,修改要扫描的包和引用dubbo服务两项配置,要扫描的包”com.taotao.cart.controller”我们是需要新建的,如下图所示。
淘淘商城——购物车实现分析及工程搭建_第7张图片
为了大家方便复制,现把该文件的内容贴出。


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    
    <context:property-placeholder location="classpath:resource/resource.properties" />

    <context:component-scan base-package="com.taotao.cart.controller" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    bean>

    
    <dubbo:application name="taotao-cart-web"/>
    <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>    
    

beans>

最后来配置web.xml,我们可参考taotao-portal-web工程的web.xml,首先需要在webapp目录下新建一个WEB-INF目录,并拷贝taotao-portal-web工程的web.xml文件到WEB-INF目录下,我们需要修改的地方是名字,即把原来所有的”taotao-portal-web”都更改为”taotao-cart-web”(可以使用全文替换)。
这样,我们的taotao-cart-web工程便搭建完了。

你可能感兴趣的:(淘淘商城)