ssm(Spring、Springmvc、Mybatis)實戰之淘淘商城-第五天(非原創)


文章大綱

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

 

一、課程介紹

一共14天課程
(1)第一天:電商行業的背景。淘淘商城的介紹。搭建項目工程。Svn的使用。
(2)第二天:框架的整合。后台管理商品列表的實現。分頁插件。
(3)第三天:后台管理。商品添加。商品類目的選擇、圖片上傳、富文本編輯器的使用。
(4)第四天:商品規格的實現。
(5)第五天:商城前台系統的搭建。首頁商品分類的展示。Jsonp。
(6)第六天:cms系統的實現。前台大廣告位的展示。
(7)第七天:cms系統添加緩存。Redis。緩存同步。
(8)第八天:搜索功能的實現。使用solr實現搜索。
(9)第九天:商品詳情頁面的展示。
(10)第十天:單點登錄系統。Session共享。
(11)第十一天:購物車訂單系統的實現。
(12)第十二天:nginx。反向代理工具。
(13)第十三天:redis集群的搭建、solr集群的搭建。系統的部署。
(14)項目總結。

二、前台系統(門戶系統)搭建介紹

1. 什么是門戶系統

  廣義上的門戶就是將各種應用系統、數據資源和互聯網資源集成到一個信息管理平台之上,並以統一的用戶界面提供給用戶,並建立企業對客戶、企業對內部員工和企業對企業的信息通道。
  簡單來說就是網站的入口,淘淘商城門戶系統首頁如下圖所示:

 

2. 淘淘商城優化后技術架構

 
 

優點
(1)前台系統和服務層可以分開,降低系統的耦合度。
(2)開發團隊可以分開,提高開發效率
(3)系統分開可以靈活的進行分布式部署。
缺點
(1)服務之間通信使用接口通信,開發工作量提高。
(2)前台系統分為兩部分,一部分是服務層web工程,功能就是發布服務
(3)另外一部分:表現層,展示頁面,沒有業務邏輯。所有業務邏輯就是調用服務層的服務。

3. 所使用技術

Srping + SpringMVC+Mybatis
JSP+JS + CSS

三、前台系統(門戶系統)搭建實戰

1. taotao-rest服務層搭建

1.1 創建項目

 
 
 
 

創建后項目結構如下

 

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,這里直接體現配置后的文件內容
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 創建后的項目結構如下

 

2. taotao-portal門戶層搭建

2.1 項目創建

 
 
 
 

創建后項目結構如下

 

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> <!-- 配置一個偽靜態的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/ #單點登錄系統的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 #訂單系統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分發請求所必需的,並且提供了數據綁定支持 --> <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 創建后的項目結構如下

 

3. 安裝taotao-manager到本地倉庫

  在完成上述項目創建后,大家會發現taotao-rest和taotao-portal項目都報錯了,因為找不到taotao-manager的依賴,所以下面我們需要將taotao-manager項目安裝到本地倉庫中(與taotao-rest和taotao-portal所配置的maven倉庫是同一個)

3.1 安裝命令
Maven命令:install -DskipTests(在工具中,無需出現mvn前綴)
跳過測試

3.2 安裝實戰

我的本地倉庫目前只有這幾個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 訪問門戶系統

 

四、js請求跨域解決

1 什么是跨域

  Js是不能跨域請求。出於安全考慮,js設計時不可以跨域。如果跨域了,會出現以下內容:

 

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

 

2 跨域解決方法

  跨域解決方法很多,包括攔截器下允許所有域名訪問、jsonp、修改document.domain來跨子域等等,下面我們重點講解攔截器下允許所有域名訪問,jsonp的使用可以在項目源碼與資料下載中進行學習

3 攔截器下允許所有域名訪問實戰

@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為設置預檢可以被緩存多久
瀏覽器的同源策略,就是出於安全考慮,瀏覽器會限制從腳本發起的跨域HTTP請求(比如異步請求GET, POST, PUT, DELETE, OPTIONS等等),所以瀏覽器會向所請求的服務器發起兩次請求,第一次是瀏覽器使用OPTIONS方法發起一個預檢請求,第二次才是真正的異步請求,第一次的預檢請求獲知服務器是否允許該跨域請求:如果允許,才發起第二次真實的請求;如果不允許,則攔截第二次請求。
Access-Control-Max-Age用來指定本次預檢請求的有效期,單位為秒,,在此期間不用發出另一條預檢請求。
例如:
resp.addHeader("Access-Control-Max-Age", "0"),表示每次異步請求都發起預檢請求,也就是說,發送兩次請求。
resp.addHeader("Access-Control-Max-Age", "1800"),表示隔30分鍾才發起預檢請求。也就是說,發送兩次請求
(4)Access-Control-Allow-Credentials表示是否允許客戶端攜帶驗證信息
例如 cookie 之類的。這樣客戶端在發起跨域請求的時候,不就可以攜帶允許的頭,還可以攜帶驗證信息的頭,如果客戶端請求框架是 axios,並且手殘的設置了 withCredentials: true,意思是客戶端想要攜帶驗證信息頭,但是服務端設置是 'supportsCredentials' => false, ,表示不允許攜帶信息頭,就會出問題的
(5)對於驗證要去不是非常嚴格情況下來講,只需要添加Access-Control-Allow-Origin即可

五、項目源碼與資料下載

鏈接:https://pan.baidu.com/s/1T3sG4pmfbQ75DVw8kW69Gg
提取碼:7qm4

六、參考文章

http://yun.itheima.com/course?hm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM