文章大綱
一、課程介紹
二、前臺系統(tǒng)(門戶系統(tǒng))搭建介紹
三、前臺系統(tǒng)(門戶系統(tǒng))搭建實戰(zhàn)
四、js請求跨域解決
五、項目源碼與資料下載
六、參考文章

一、課程介紹
一共14天課程
(1)第一天:電商行業(yè)的背景。淘淘商城的介紹。搭建項目工程。Svn的使用。
(2)第二天:框架的整合。后臺管理商品列表的實現(xiàn)。分頁插件。
(3)第三天:后臺管理。商品添加。商品類目的選擇、圖片上傳、富文本編輯器的使用。
(4)第四天:商品規(guī)格的實現(xiàn)。
(5)第五天:商城前臺系統(tǒng)的搭建。首頁商品分類的展示。Jsonp。
(6)第六天:cms系統(tǒng)的實現(xiàn)。前臺大廣告位的展示。
(7)第七天:cms系統(tǒng)添加緩存。Redis。緩存同步。
(8)第八天:搜索功能的實現(xiàn)。使用solr實現(xiàn)搜索。
(9)第九天:商品詳情頁面的展示。
(10)第十天:單點登錄系統(tǒng)。Session共享。
(11)第十一天:購物車訂單系統(tǒng)的實現(xiàn)。
(12)第十二天:nginx。反向代理工具。
(13)第十三天:redis集群的搭建、solr集群的搭建。系統(tǒng)的部署。
(14)項目總結。
二、前臺系統(tǒng)(門戶系統(tǒng))搭建介紹
1. 什么是門戶系統(tǒng)
??廣義上的門戶就是將各種應用系統(tǒng)、數據資源和互聯(lián)網資源集成到一個信息管理平臺之上,并以統(tǒng)一的用戶界面提供給用戶,并建立企業(yè)對客戶、企業(yè)對內部員工和企業(yè)對企業(yè)的信息通道。
??簡單來說就是網站的入口,淘淘商城門戶系統(tǒng)首頁如下圖所示:

2. 淘淘商城優(yōu)化后技術架構


優(yōu)點
(1)前臺系統(tǒng)和服務層可以分開,降低系統(tǒng)的耦合度。
(2)開發(fā)團隊可以分開,提高開發(fā)效率
(3)系統(tǒng)分開可以靈活的進行分布式部署。
缺點
(1)服務之間通信使用接口通信,開發(fā)工作量提高。
(2)前臺系統(tǒng)分為兩部分,一部分是服務層web工程,功能就是發(fā)布服務
(3)另外一部分:表現(xiàn)層,展示頁面,沒有業(yè)務邏輯。所有業(yè)務邏輯就是調用服務層的服務。
3. 所使用技術
Srping + SpringMVC+Mybatis
JSP+JS + CSS
三、前臺系統(tǒng)(門戶系統(tǒng))搭建實戰(zhàn)
1. taotao-rest服務層搭建
1.1 創(chuàng)建項目




創(chuàng)建后項目結構如下

1.2 pom文件添加配置
<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.0</modelVersion>
<parent>
<groupId>com.wxc</groupId>
<artifactId>taotao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.taotao</groupId>
<artifactId>taotao-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 依賴taotao-manager的pojo模塊 -->
<dependency>
<groupId>com.wxc</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wxc</groupId>
<artifactId>taotao-manager-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wxc</groupId>
<artifactId>taotao-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- JSP相關 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Redis客戶端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8081</port>
<path>/</path>
<!-- <url>http://192.168.25.136:8080/manager/text</url>
<username>tomcat</username>
<password>tomcat</password> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
1.3 web.xml文件添加相關配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="taotao" version="2.5">
<display-name>taotao-rest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解決post亂碼 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!-- <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>taotao-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
1.4 resources文件夾下相關配置
??相關的配置過程可以參考taotao-manager-web,這里直接體現(xiàn)配置后的文件內容
mybatis文件夾下新建SqlMapConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
properties文件夾下新建db.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=147258qq
spring文件夾下新建applicationContext-dao.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 數據庫連接池 -->
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 數據庫連接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 配置SqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加載mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
<!-- 配置數據源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置包掃描器,掃描mapper接口生成代理對象放到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要掃描的包 -->
<property name="basePackage" value="com.taotao.mapper"/>
</bean>
</beans>
spring文件夾下新建applicationContext-service.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置包掃描器,掃描@Service主鍵的類 -->
<context:component-scan base-package="com.taotao.rest.service"/>
</beans>
spring文件夾下新建applicationContext-trans.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事務配置 -->
<!-- 事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數據源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.taotao.rest.service.*.*(..))" />
</aop:config>
</beans>
spring文件夾下新建springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驅動 -->
<mvc:annotation-driven />
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 包掃描器,掃描@Controller注解 -->
<context:component-scan base-package="com.taotao.rest.controller" />
</beans>
1.5 創(chuàng)建后的項目結構如下

2. taotao-portal門戶層搭建
2.1 項目創(chuàng)建




創(chuàng)建后項目結構如下

2.2 pom文件添加配置
<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.0</modelVersion>
<parent>
<groupId>com.wxc</groupId>
<artifactId>taotao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.taotao</groupId>
<artifactId>taotao-portal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 依賴taotao-common工程 -->
<dependency>
<groupId>com.wxc</groupId>
<artifactId>taotao-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wxc</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- JSP相關 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 單元測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8082</port>
<path>/</path>
<!-- <url>http://192.168.25.137:8080/manager/text</url>
<username>tomcat</username>
<password>tomcat</password> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3 web.xml文件添加配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="taotao" version="2.5">
<display-name>taotao-portal</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解決post亂碼 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!-- <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>taotao-portal</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-portal</servlet-name>
<!-- 配置一個偽靜態(tài)的url攔截形式 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>taotao-portal</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
2.4 resources文件夾添加相關配置
properties文件夾下新建resource.properties配置文件
#服務層基礎url
SERVICE_BASE_URL=http://192.168.101.6:8081/rest
#大廣告位服務url
INDEX_AD1_URL=/content/category/
#搜索服務url
SEARCH_BASE_URL=http://192.168.101.6:8081/search/q
#取商品信息URL
ITEM_BASE_URL=/item/id/
ITEM_DESC_URL=/item/desc/
ITEM_PARAM_URL=/item/param/
#單點登錄系統(tǒng)的url
SSO_BASE_URL=http://192.168.101.6:8082
SSO_REDIRICT_URL=http://sso.taotao.com
#根據token取用戶的服務
SSO_TOKEN_USER_URL=/user/token/
#SSO\u767b\u5f55\u9875\u9762url
SSO_LOGIN_PAGE_URL=/user/page/login
#購物車Cookie的有效時間60*60*24*7
CAT_COOKIE_EXPIRE=604800
#訂單系統(tǒng)url
ORDER_BASE_URL=http://192.168.101.6:8083/order
#提交訂單url
ORDER_CREATE_URL=/create
spring文件夾下新建applicationContext-service.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置包掃描器,掃描@Service主鍵的類 -->
<context:component-scan base-package="com.taotao.portal.service"/>
<context:property-placeholder location="classpath:properties/*.properties" />
</beans>
spring文件夾下新建springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驅動
<mvc:annotation-driven>會自動注冊RequestMappingHandlerMapping與RequestMappingHandlerAdapter兩個Bean,
這是Spring MVC為@Controller分發(fā)請求所必需的,并且提供了數據綁定支持 -->
<mvc:annotation-driven />
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 包掃描器,掃描@Controller注解 -->
<context:component-scan base-package="com.taotao.portal.controller" />
<!-- 攔截器配置 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 攔截訂單類請求 -->
<mvc:mapping path="/order/**"/>
<bean class="com.taotao.portal.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
2.5 webapp下增加css、js、jsp資源文件
??在項目源碼與資料下載的文件夾中找到資源文件,并復制在項目如下位置

2.6 創(chuàng)建后的項目結構如下

3. 安裝taotao-manager到本地倉庫
??在完成上述項目創(chuàng)建后,大家會發(fā)現(xiàn)taotao-rest和taotao-portal項目都報錯了,因為找不到taotao-manager的依賴,所以下面我們需要將taotao-manager項目安裝到本地倉庫中(與taotao-rest和taotao-portal所配置的maven倉庫是同一個)
3.1 安裝命令
Maven命令:install -DskipTests(在工具中,無需出現(xiàn)mvn前綴)
跳過測試
3.2 安裝實戰(zhàn)
我的本地倉庫目前只有這幾個maven依賴包

在taotao-manager中配置相關maven命令

運行命令


經過該操作后,兩個項目不會再報錯,如果還會,請關閉項目重新打開即可。
4. 配置項目并啟動
4.1 修改訪問配置
??在taotao-rest中,我們使用的是8081端口,在taotao-portal中使用的是8082,taotao-rest中提供服務,taotao-portal訪問服務(前后端分離項目中,taotao-portal為用戶端),所以我們需要配置taotao-portal訪問taotao-rest的地址

4.2 啟動taotao-rest項目


4.3 啟動taotao-portal項目


4.4 訪問門戶系統(tǒng)

四、js請求跨域解決
1 什么是跨域
??Js是不能跨域請求。出于安全考慮,js設計時不可以跨域。如果跨域了,會出現(xiàn)以下內容:

什么是跨域:
(1)域名不同時。
(2)域名相同,端口不同。
只有域名相同、端口相同時,才可以訪問。

2 跨域解決方法
??跨域解決方法很多,包括攔截器下允許所有域名訪問、jsonp、修改document.domain來跨子域等等,下面我們重點講解攔截器下允許所有域名訪問,jsonp的使用可以在項目源碼與資料下載中進行學習
3 攔截器下允許所有域名訪問實戰(zhàn)
@Component
public class CORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//添加跨域CORS
response.setHeader("Access-Control-Allow-Origin","*"); //允許所有域名訪問
response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
return true;
}
}
溫馨提示:
(1)Access-Control-Allow-Origin為設置允許所有域名訪問,在這里,我們可以指定相應的域名
(2)Access-Control-Allow-Methods為設置允許跨域的方法
(3)Access-Control-Max-Age為設置預檢可以被緩存多久
瀏覽器的同源策略,就是出于安全考慮,瀏覽器會限制從腳本發(fā)起的跨域HTTP請求(比如異步請求GET, POST, PUT, DELETE, OPTIONS等等),所以瀏覽器會向所請求的服務器發(fā)起兩次請求,第一次是瀏覽器使用OPTIONS方法發(fā)起一個預檢請求,第二次才是真正的異步請求,第一次的預檢請求獲知服務器是否允許該跨域請求:如果允許,才發(fā)起第二次真實的請求;如果不允許,則攔截第二次請求。
Access-Control-Max-Age用來指定本次預檢請求的有效期,單位為秒,,在此期間不用發(fā)出另一條預檢請求。
例如:
resp.addHeader("Access-Control-Max-Age", "0"),表示每次異步請求都發(fā)起預檢請求,也就是說,發(fā)送兩次請求。
resp.addHeader("Access-Control-Max-Age", "1800"),表示隔30分鐘才發(fā)起預檢請求。也就是說,發(fā)送兩次請求
(4)Access-Control-Allow-Credentials表示是否允許客戶端攜帶驗證信息
例如 cookie 之類的。這樣客戶端在發(fā)起跨域請求的時候,不就可以攜帶允許的頭,還可以攜帶驗證信息的頭,如果客戶端請求框架是 axios,并且手殘的設置了 withCredentials: true,意思是客戶端想要攜帶驗證信息頭,但是服務端設置是 'supportsCredentials' => false, ,表示不允許攜帶信息頭,就會出問題的
(5)對于驗證要去不是非常嚴格情況下來講,只需要添加Access-Control-Allow-Origin即可
五、項目源碼與資料下載
鏈接:https://pan.baidu.com/s/1T3sG4pmfbQ75DVw8kW69Gg
提取碼:7qm4