本項目是@尚硅谷相關視頻的記錄。html
本項目使用Maven構建,工程架構以下圖所示:前端
1、公司的公共父工程和工具類包
1.父工程
每一個公司都有本身的父工程java
父工程做用:對公司使用的jar包進行統一管理,別的工程須要繼承公司的父工程mysql
父工程必須是pomgit
項目的打包類型:pom、jar、wargithub
pom ---------> 父類型都爲pom類型web
jar ---------> 內部調用或者是做服務使用spring
war ---------> 須要部署的項目sql
packing默認是jar類型數據庫
項目中通常使用maven進行模塊管理,每一個模塊下對應都有一個pom文件,pom文件中維護了各模塊之間的依賴和繼承關係。項目模塊化能夠將通用的部分抽離出來,方便重用;修改一部分代碼再也不是build整個項目,縮短了build時間;此外各模塊都有本身的pom文件,結構更清晰。
pom文件除了GAV(groupId, artifactId, version)是必需要配置的,另外一個重要的屬性就是packaging打包類型,全部的父級項目的packaging都爲pom,packaging默認是jar類型,若是不做配置,maven會將該項目打成jar包。
使用maven進行模塊劃分管理,通常都會有一個父級項目,做爲父級項目,還有一個重要的屬性,那就是modules,經過modules標籤將項目的全部子項目引用進來,在build父級項目時,會根據子模塊的相互依賴關係整理一個build順序,而後依次build。
而對於各個子項目,須要在其對應的pom文件開頭申明對父級項目的引用,經過GAV實現。對於子項目本身的GAV配置,GV若是不配置,則會從父級項目的配置繼承過來。子模塊可經過dependencies標籤來添加本身的依賴,此外子類項目的packaging值只能是war或者jar。若是是須要部署的項目,則須要打包成war類型,若是隻是內部調用或者是做服務使用,則推薦打包成jar類型。
1)建立新的工做集,命名爲scw
在Eclipse中使用Working set管理項目 https://jingyan.baidu.com/article/e4511cf343a3f22b845eaf21.html
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> <groupId>com.atguigu.project</groupId> <artifactId>project-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 父工程中作依賴管理:jar包的版本控制等都在這裏 --> <properties> <!-- 公共依賴 --> <commons-io.version>2.5</commons-io.version> <commons-lang3.version>3.6</commons-lang3.version> <commons-codec.version>1.10</commons-codec.version> <commons-beanutils.version>1.9.3</commons-beanutils.version> <commons-collections.version>3.2.2</commons-collections.version> <commons-math3.version>3.6.1</commons-math3.version> <commons.fileupload>1.3.2</commons.fileupload> <commons-email.version>1.4</commons-email.version> <!-- junit --> <junit.version>4.12</junit.version> <!-- 時間日期操做 --> <joda-time.version>2.9.9</joda-time.version> <!-- httpclient --> <httpclient.version>4.5.3</httpclient.version> <!-- 功能組件 --> <poi.version>3.16</poi.version> <quartz.version>2.2.3</quartz.version> <!-- 數據庫 --> <!--druid:德魯伊,數據庫鏈接池 --> <druid.version>1.1.0</druid.version> <mysql.connector>5.1.42</mysql.connector> <!-- 基礎框架 --> <spring.version>4.3.8.RELEASE</spring.version> <mybatis.version>3.4.3</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <!-- 分頁 --> <pagehelper.version>5.0.3</pagehelper.version> <!-- jackson --> <jackson.version>2.7.4</jackson.version> <!-- MBG --> <mbg.version>1.3.5</mbg.version> <!-- 日誌 --> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.6</slf4j.version> <!-- servlet-api,jsp-api,jstl --> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.2</jsp-api.version> <jstl.version>1.2</jstl.version> <!-- email,commons,httpclient...activiti... --> <activiti.version>5.22.0</activiti.version> <activiti.spring.version>5.22.0</activiti.spring.version> </properties> <!-- 依賴管理 --> <dependencyManagement> <dependencies> <!-- 公共依賴 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec.version}</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>${commons-beanutils.version}</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>${commons-collections.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>${commons-math3.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons.fileupload}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>${commons-email.version}</version> </dependency> <!--公共依賴結束 --> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 時間日期 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <!-- 其餘功能性組件 --> <!-- poi:文檔操做 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <!-- quartz:石英調度 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency> <!-- 數據庫模塊 --> <!-- 鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- 驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector}</version> </dependency> <!-- 基礎框架 --> <!-- Spring配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis配置 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 基礎框架完成 --> <!-- 分頁 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mbg.version}</version> </dependency> <!-- 工做流 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti.spring.version}</version> </dependency> <!-- 日誌 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!-- 依賴的WEB類庫 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
Maven中POM.XML詳解 http://www.javashuo.com/article/p-rciuhgau-dv.html
這個pom.xml主要由三部分組成:GAV等信息,properties和dependencyManagement,properties指定了依賴的版本,dependencyManagement指定了依賴。
用到的依賴:
公共依賴 | commons-io |
commons-lang3 | |
commons-codec | |
commons-beanutils | |
commons-collections | |
commons-math3 | |
commons-fileupload | |
commons-email | |
數據庫 | druid 鏈接池 |
mysql-connector-java 驅動 | |
基礎框架 | Spring |
mybatis | |
WEB類庫 | jsp-api |
servlet-api | |
jstl | |
日誌 | log4j |
slf4j-log4j12 | |
slf4j-api | |
工做流 | activiti-engine |
activiti-spring | |
其餘 | mybatis-generator-core |
jackson | |
pagehelper 分頁 | |
junit | |
joda-time 時間 | |
httpclient | |
poi 文檔操做 | |
quartz 石英調度 |
2.公司工具類的包
公司除了父工程之外,終年累月還會積累不少工具類,這些經常使用的工具類會存放在同一個地方。
創建新的maven工程
<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> <groupId>com.atguigu.project</groupId> <artifactId>project-commons</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 公司工具類 --> <parent> <groupId>com.atguigu.project</groupId> <artifactId>project-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- 導入公共依賴的一些包 --> <dependencies> <!-- 公共依賴 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> </dependency> <!--公共依賴結束 --> </dependencies> </project>
繼承公共父工程
該包可能會用到公共依賴,引入公共依賴
2、項目的父工程
下面開始咱們本身工程的構建
咱們的項目也須要一個父工程,scw-parent,選擇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.atguigu.project</groupId> <artifactId>project-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.atguigu.scw</groupId> <artifactId>scw-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> </project>
繼承公司的公共父工程project-parent
3、項目分模塊
咱們的項目是分模塊構建的
新建一個項目,這個項目聚合了後臺功能的各個模塊,所以打包方式要選pom,這樣才能夠在該項目下建立maven模塊
pom.xml
<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> <groupId>com.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>com.atguigu.scw</groupId> <artifactId>scw-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modules> <module>manager-pojo</module> <module>manager-dao</module> <module>manager-service</module> <module>manager-web</module> </modules> </project>
繼承項目父工程scw-parent
項目分爲四個模塊manager-pojo、manager-dao、manager-service和manager-web
pojo裏是經常使用的JavaBean
service裏是業務邏輯
dao是數據庫相關
4、建立模塊
一共四個模塊manager-pojo、manager-dao、manager-service和manager-web
manager-web要選擇war包,一個聚合父項目應該只有一個war包
右鍵web項目,選擇Build Path,選擇Configure Build Path...
選Project Facets
Eclipse的Project Facets屬性
咱們建立了war包,可是web項目還沒配置好,還要從新選擇並配置Dynamic Web Mobile,設置webapp的路徑。
取消Dynamic Web Mobile,點apply,再從新選中
出現下面紅線內容,點擊
Content directory輸入src/main/webapp
webapp在項目中的位置以下圖:
5、各模塊間依賴的配置
四個模塊共同點:都要繼承scw-manager,所以都有:
<parent> <groupId>com.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
dao依賴pojo,service依賴dao,web依賴service
dao的pom.xml
沒必要配置依賴
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-pojo</artifactId> </project>
dao依賴pojo
dao的pom.xml
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-dao</artifactId> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
service依賴dao
service的pom.xml
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-service</artifactId> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
web依賴service
web的pom.xml
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-web</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
聚合好後,各個項目依賴關係創建好,依賴的模塊可使用被依賴模塊的jar包,如commons引入service後,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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-dao</artifactId> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 和數據庫有關的放在該層 --> <!-- 數據庫模塊 --> <!-- 鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!-- 驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis配置 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> </dependency> </dependencies> </project>
service引入公司的工具類commons依賴和IOC框架,除MVC覺得的Spring框架
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-service</artifactId> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.atguigu.project</groupId> <artifactId>project-commons</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 存放IOC框架 --> <!-- Spring配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> </dependencies> </project>
web引入其餘jar包
<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.atguigu.scw</groupId> <artifactId>scw-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>manager-web</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.atguigu.scw</groupId> <artifactId>manager-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- 分頁 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <!-- 日誌 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <!-- 依賴的WEB類庫 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> </project>
項目完整關係圖:
6、測試
pojo下建立Person
package com.atguigu.scw.manager.bean; public class Person {}
web下建立HelloController
package com.atguigu.scw.manager.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.atguigu.scw.manager.bean.Person; @Controller public class HelloController { @RequestMapping("hello") public String hello() { Person person = new Person(); return "/success.jsp"; } }
web src/main/resources下建立Spring配置文件
<?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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="com.atguigu"></context:component-scan> </beans>
webapp下
建立index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="hello">hello</a> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>你好</h1> </body> </html>
前端控制器
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
運行tomcat,瀏覽器訪問