淘淘商城——搭建商品详情页面展示工程

港控/mmm° 2022-05-23 01:46 354阅读 0赞

从淘淘商城的系统架构上来说,其实商品详情展示与搜索系统都是独立的表现层工程。我们以京东为例,当我们搜索到某商品后,点击那个商品链接,进入商品详情页面时,可以在浏览器地址栏看到”item.jd.com”,很明显不是搜索系统工程了,这是专门用来展示商品详情的工程。
这里写图片描述
我们同样也需要这么一个展示商品详情的工程,现在我们就新建一个taotao-item-web工程,该工程可参考taotao-search-web工程来搭建哟!
首先点击【File】菜单选项,并在下拉框中选中【New】,接着点击【Other】,如下:
这里写图片描述
在输入框中输入maven,并选择Maven Project,如下:
这里写图片描述
点击【Next】,勾选Create a simple project复选框,如果你不打上这个勾,它会让你选择一个骨架,但骨架里面是没有pom这个模板的。
这里写图片描述
点击【Next】,出现如下对话框,在该对话框中定义maven工程的坐标,如下:
这里写图片描述
注意:taotao-item-web工程的打包方式是war,且须依赖父工程。
最后点击【Finish】,taotao-item-web工程就创建好了,但是新建的web工程由于缺少web.xml文件而报错,解决方法是在webapp目录下新建一个WEB-INF目录,并在该目录下新建web.xml文件,至于该文件的内容具体是什么,后面会具体给出,这里我们并不着急。
既然taotao-item-web与taotao-search-web都是web工程,我们可以参考taotao-search-web工程进行配置。首先是配置taotao-item-web工程的pom.xml文件,我们把taotao-search-web工程的pom.xml文件的依赖拷贝过来,然后稍作修改,修改后pom.xml文件的内容如下,其中依赖的接口工程由原来的taotao-search-interface修改为taotao-manager-interface,因为商品详情肯定是要查询商品服务的。还有就是最后配置的tomcat启动端口改为8086,因为8080到8085都已经被占用了。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.taotao</groupId>
  6. <artifactId>taotao-parent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. </parent>
  9. <groupId>com.taotao</groupId>
  10. <artifactId>taotao-item-web</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <packaging>war</packaging>
  13. <dependencies>
  14. <!-- 依赖taotao-common -->
  15. <dependency>
  16. <groupId>com.taotao</groupId>
  17. <artifactId>taotao-common</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. </dependency>
  20. <!-- 依赖taotao-manager-interface -->
  21. <dependency>
  22. <groupId>com.taotao</groupId>
  23. <artifactId>taotao-manager-interface</artifactId>
  24. <version>0.0.1-SNAPSHOT</version>
  25. </dependency>
  26. <!-- Spring -->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-context</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-beans</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-webmvc</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-jdbc</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-aspects</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-jms</artifactId>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-context-support</artifactId>
  54. </dependency>
  55. <!-- JSP相关 -->
  56. <dependency>
  57. <groupId>jstl</groupId>
  58. <artifactId>jstl</artifactId>
  59. </dependency>
  60. <dependency>
  61. <groupId>javax.servlet</groupId>
  62. <artifactId>servlet-api</artifactId>
  63. <scope>provided</scope>
  64. </dependency>
  65. <dependency>
  66. <groupId>javax.servlet</groupId>
  67. <artifactId>jsp-api</artifactId>
  68. <scope>provided</scope>
  69. </dependency>
  70. <!-- dubbo相关 -->
  71. <dependency>
  72. <groupId>com.alibaba</groupId>
  73. <artifactId>dubbo</artifactId>
  74. <!-- 排除依赖 -->
  75. <exclusions>
  76. <exclusion>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring</artifactId>
  79. </exclusion>
  80. <exclusion>
  81. <groupId>org.jboss.netty</groupId>
  82. <artifactId>netty</artifactId>
  83. </exclusion>
  84. </exclusions>
  85. </dependency>
  86. <!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
  87. <dependency>
  88. <groupId>org.apache.zookeeper</groupId>
  89. <artifactId>zookeeper</artifactId>
  90. </dependency>
  91. <dependency>
  92. <groupId>com.github.sgroschupf</groupId>
  93. <artifactId>zkclient</artifactId>
  94. </dependency>
  95. <dependency>
  96. <groupId>junit</groupId>
  97. <artifactId>junit</artifactId>
  98. </dependency>
  99. </dependencies>
  100. <!-- 配置tomcat插件 -->
  101. <build>
  102. <plugins>
  103. <plugin>
  104. <groupId>org.apache.tomcat.maven</groupId>
  105. <artifactId>tomcat7-maven-plugin</artifactId>
  106. <configuration>
  107. <port>8086</port>
  108. <path>/</path>
  109. </configuration>
  110. </plugin>
  111. </plugins>
  112. </build>
  113. </project>

紧接着来配置资源文件,我们把taotao-search-web工程的src/main/resources目录下的resource和spring目录拷贝到taotao-item-web工程的src/main/resources目录下,其中resource目录下的resource.properties文件内容删光,以备以后使用该文件,如下图所示。
这里写图片描述
下面打开spring目录下的springmvc.xml,修改下要扫描的包,修改为com.taotao.item.controller(这个包我们要在taotao-item-web工程中新建),引用的dubbo服务名称要改为”taotao-item-web”,如下图所示。
这里写图片描述
为了大家方便复制,现将springmvc.xml文件的内容贴出。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  9. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  11. <!-- 加载属性文件 -->
  12. <context:property-placeholder location="classpath:resource/resource.properties" />
  13. <context:component-scan base-package="com.taotao.item.controller" />
  14. <mvc:annotation-driven />
  15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/WEB-INF/jsp/" />
  17. <property name="suffix" value=".jsp" />
  18. </bean>
  19. <!-- 引用dubbo服务 -->
  20. <dubbo:application name="taotao-item-web"/>
  21. <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>
  22. <!-- <dubbo:reference interface="com.taotao.content.service.ContentService" id="contentService" /> -->
  23. </beans>

下面我们需要把taotao-search-web工程的web.xml拷贝到taotao-item-web工程下(当然,我们需要先建一个WEB-INF目录),拷贝过来后需要修改的地方如下图所示。
这里写图片描述
同样为了大家方便复制,现将web.xml文件的内容贴出。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>taotao-item-web</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. </welcome-file-list>
  10. <!-- 解决post乱码 -->
  11. <filter>
  12. <filter-name>CharacterEncodingFilter</filter-name>
  13. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  14. <init-param>
  15. <param-name>encoding</param-name>
  16. <param-value>utf-8</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>CharacterEncodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <!-- springmvc的前端控制器 -->
  24. <servlet>
  25. <servlet-name>taotao-item-web</servlet-name>
  26. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  27. <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
  28. <init-param>
  29. <param-name>contextConfigLocation</param-name>
  30. <param-value>classpath:spring/springmvc.xml</param-value>
  31. </init-param>
  32. <load-on-startup>1</load-on-startup>
  33. </servlet>
  34. <servlet-mapping>
  35. <servlet-name>taotao-item-web</servlet-name>
  36. <!-- 伪静态化,seo搜索引擎优化 -->
  37. <url-pattern>*.html</url-pattern>
  38. </servlet-mapping>
  39. </web-app>

接着我们把下图所示的商品详情静态页面放到商品详情展示工程当中。
这里写图片描述
记得将css、js、images放到webapp目录下,将jsp目录放到WEB-INF目录下,如下图所示。
这里写图片描述
下面我们依次启动taotao-manager、taotao-content、taotao-search、taotao-portal-web、taotao-search-web这5个工程,前提是要保证zookeeper、Redis(我这里用的是单机版)、image、Solr(我这里用的是集群版)、ActiveMQ服务都开启。启动好之后,我们访问淘淘商城首页,搜索我们上文所添加的锤子手机,然后点击它进入详情页面,会发现如下图所示的错误,这是由于访问的工程不正确造成的。
这里写图片描述
我们找到taotao-search-web工程下的search.jsp页面,在下图标示的地方将原来的相对路径改为绝对路径,就是在前面加上http://localhost:8086,如下图所示。
这里写图片描述
修改完后保存,刷新搜索工程页面,然后再点击手机进入详情页面,可以看到端口号已经变为8086了。只是由于商品详情工程还没有开发完也没有启动,因此现在还无法访问。
这里写图片描述
这样,商品详情展示工程我们便搭建好了。

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读