所使用的技術
後台技術: Spring SpringMVC Mybatis(不用)。
前台技術:html、CSS、JS
如果不使用Mybatis,商品的資料從何而來。 – 來源於Mysql資料庫
擷取資料的途徑:
1、 從JDBC擷取
a) 優點
i. 直接,擷取的途徑較短,簡單
b) 缺點
i. 對後台系統團隊而言,資料不安全(只要開放的賬戶是唯讀賬戶即可)
ii. 前端系統團隊需要有學習的成本,才能使用資料庫
iii. 依賴、耦合度太高,後端團隊將資料庫結構修改,那麼其他團隊必須跟著修改邏輯,才能使用
iv. 直接走資料庫查詢,無法添加緩衝邏輯
2、 通過後台系統介面擷取
a) 優點
i. 耦合度降低,後端團隊只要保證介面的返回資料格式不變化,其他團隊就無需升級
ii. 資料安全
iii. 前端團隊無需瞭解學習後端團隊的底層資料庫結構
iv. 後端團隊可以在介面處添加緩衝邏輯
b) 缺點
i. 擷取的路徑較長(不是真正的缺點) 建立taotao-web
匯入依賴
<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.taotao.parent</groupId> <artifactId>taotao-parent</artifactId> <version>0.0.1-SNAPSHOT</version></parent> <groupId>com.taotao.web</groupId> <artifactId>taotao-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging><dependencies> <dependency> <groupId>com.taotao-common</groupId> <artifactId>taotao-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- Jackson Json處理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</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> <!-- Apache工具組件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 配置Tomcat外掛程式 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8082</port> <path>/</path> </configuration> </plugin> </plugins> </build></project>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>taotao-web</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> </context-param> <!--Spring的ApplicationContext 載入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 編碼過濾器,以UTF8編碼 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC架構入口 --> <servlet> <servlet-name>taotao-web</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/taotao-web-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>taotao-web</servlet-name> <!-- 偽靜態 有利於做SEO(搜尋引擎最佳化) --> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Spring和SpringMVC設定檔
<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"> <!-- 使用spring內建的預留位置替換功能 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 允許JVM參數覆蓋 --> <!-- java -Djdbc.url=123 -jar xxx.jar --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <!-- 忽略沒有找到的資源檔 --> <property name="ignoreResourceNotFound" value="true" /> <!-- 配置資源檔 --> <property name="locations"> <list> <value>classpath:env.properties</value> </list> </property> </bean> <!-- 掃描包 --> <context:component-scan base-package="com.taotao"/></beans>
<?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-4.0.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-4.0.xsd"> <!-- 註解驅動 --> <mvc:annotation-driven/> <!-- 掃描Controller --> <context:component-scan base-package="com.taotao.web.controller"/> <!-- 視圖解析器 --> <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean></beans>
匯入靜態檔案
編寫IndexController
package com.taotao.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@RequestMapping("index")@Controllerpublic class IndexController { /** * 首頁 * * @return */ @RequestMapping(method = RequestMethod.GET) public ModelAndView index() { ModelAndView mv = new ModelAndView("index"); return mv; }}
配置hosts和nginx
運行測試發現:
www.taotao.com會找不到頁面:
www.taotao.com/index.html才找得到頁面
因為輸入www.taotao.com,預設找的是index.jsp,不滿足*.html的規則。
修改後重新測試: