在上一篇我們介紹瞭如何在IDEA中使用MAVEN,以及如何建立依賴等。那麼在這一篇中,我們就試圖搭建一個生產級的解決方案,大家可以使用這個解決方案作為骨架程式碼來搭建自己的開發環境。

在這裡,我們要完成:

建立parent,在parent裡完成所有的pom依賴和定義;

建立common專案,common作為工具包而存在,被其它module所依賴;

建立dao,依賴common;

建立service,依賴dao和common;

建立web,依賴service和dao;

下面開始具體的建立過程。

1.建立Parent

所謂parent就是父工程,在這個父工程裡我們需要管理所有的子Module,所以我們將其當成是一個解決方案(solution)而存在。

首先,新建project,選擇maven。注意下圖,不要選擇archetype,

下一步,分別定義groupid,artifactid和version,

預設next,

Finish之後,來到下面的介面,

2.配置Parent依賴

開啟pom檔案,讓我們輸入,

<?xml version="1.0" encoding="UTF-8"?>
<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.</modelVersion> <groupId>com.zuikc</groupId>
<artifactId>zuikc-sln</artifactId>
<packaging>pom</packaging>
<version>1.0.</version>
<modules>
<module>zuikc-common</module>
<module>zuikc-dao</module>
<module>zuikc-service</module>
<module>zuikc-web</module>
</modules> <!-- 設定版本號 -->
<properties>
<java-version>1.10</java-version>
<javax.servlet-version>3.1.</javax.servlet-version>
<javax.servlet-jsp-version>2.2.</javax.servlet-jsp-version>
<jstl-version>1.2</jstl-version>
<taglibs-version>1.1.</taglibs-version>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties>
<!-- 統一依賴管理 -->
<dependencyManagement>
<dependencies> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-version}</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${javax.servlet-jsp-version}</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${taglibs-version}</version>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- 配置Tomcat外掛 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build> </project>

注意,這部分內容,

實際是不需要我們輸入的,隨著我們子module的建立,idea會自動為我們生成。

下面這部分內容是定義了一些屬性。由於idea預設的servlet模組是2.3的,所以需要讓我們手動定義成3.1,否則我們就使用不了servlet的註解。其次,我索性將jstl也一併引入進來。

以下這部分內容才是真正的依賴管理,

下面是定義了兩個外掛。第一個是java的編譯版本。第二個是使用tomcat外掛來執行我們即將要建立的web專案。

經過上面的設定,parent部分就大功告成了。

3.建立common

Common是工具包。

在parent上右鍵來建立子模組。如下:

注意,由於是普通jar包,所以也不要選archetype,

Next,

Next,

Finish。

建立完成後長下面這樣。

4.建立dao與service

用跟建立common一樣的方法來建立dao和service,最終結果如下:

5.建立web

接著讓我們來建立web。

這次我們要選擇“create from archetype”,如下圖選擇webapp,

Next,

Next,

Next,

Finish,

這個時候,我們發現idea的控制檯中有下圖的generating,這個時候要等幾分鐘,才能將我們的web專案初始化,

當generating完畢,web專案就會被初始化為一些預設的資料夾和檔案在裡面。當前的專案我們暫時不需要spring和日誌,所以就可以將applicaitonContext.xml和log4j.xm刪除。

6.Web的配置

接著修改web.xml,使其支援servlet3,如下,

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="false"> </web-app>

Maven預設的webapp模版沒有建立java資料夾,讓我們手動建立。手動建立完畢,發現不能在java資料夾上建立servlet,這個時候就要完成兩件事情了。

第一件事情,要將java資料夾標註為:sources root,

第二件事情要配置web的pom檔案,加入對servlet3模版的支援,如下:

<?xml version="1.0" encoding="UTF-8"?>

<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/maven-v4_0_0.xsd">
<parent>
<artifactId>zuikc-sln</artifactId>
<groupId>com.zuikc</groupId>
<version>1.0.</version>
</parent> <modelVersion>4.0.</modelVersion>
<packaging>war</packaging> <name>zuikc-web</name>
<artifactId>zuikc-web</artifactId> <dependencies> <dependency>
<groupId>com.zuikc</groupId>
<artifactId>zuikc-dao</artifactId>
<version>1.0.</version>
<scope>compile</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port></port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build> </project>

在這個pom檔案中,一是完成了servlet3的支援,而是讓專案引入引入tomcat的外掛,並指定專案在7070埠上啟動。

這個時候,還是發現不能在java上建立servlet,沒事,只要使用maven的reimport重新整理一下就行了,如下:

這個時候,就可以在java上建立servlet了,

Next,

最後ok,可以看到,

讓我們修改servlet,

package com.zuikc.servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet(name = "Servlet1", urlPatterns = "/servlet1")
public class Servlet1 extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通知瀏覽器瀏覽器用utf8來解析返回的資料
response.setHeader("Content-type", "text/html;charset=UTF-8");
//使用UTF-8轉碼
response.setCharacterEncoding("UTF-8");
response.getWriter().append("碼農星球最課程,IT培訓新選擇!");
}
}

7.配置啟動

我們要配置用maven啟動專案。如下:

確定。

然後點選run,就可以執行專案了,

注意,我們初次建立,會從maven倉儲中下載不少檔案,如下圖所示

其次,run之前需要我們將專案本身install到maven的本地倉儲中。還記得上一篇中我們是怎麼install的嗎?來來,只要在sln上install就可以了,

看到這些,就表示成功了,

現在,讓我們run這個web專案,看到這個熟悉的介面,就說明tomcat啟動成功,

來,讓我們localhost:7070/servlet1吧,

感謝關注“碼農星球”。本文版權屬於“碼農星球”。我們提供諮詢和培訓服務,關於本文有任何困惑,請關注並聯系我們。