在上一篇咱們介紹瞭如何在IDEA中使用MAVEN,以及如何建立依賴等。那麼在這一篇中,咱們就試圖搭建一個生產級的解決方案,你們可使用這個解決方案做爲骨架代碼來搭建本身的開發環境。html
在這裏,咱們要完成:java
建立parent,在parent裏完成全部的pom依賴和定義;web
建立common項目,common做爲工具包而存在,被其它module所依賴;spring
建立dao,依賴common;apache
建立service,依賴dao和common;api
建立web,依賴service和dao;瀏覽器
下面開始具體的建立過程。tomcat
1.建立Parentapp
所謂parent就是父工程,在這個父工程裏咱們須要管理全部的子Module,因此咱們將其當成是一個解決方案(solution)而存在。webapp
首先,新建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.0</modelVersion> <groupId>com.zuikc</groupId> <artifactId>zuikc-sln</artifactId> <packaging>pom</packaging> <version>1.0.1</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.0</javax.servlet-version> <javax.servlet-jsp-version>2.2.1</javax.servlet-jsp-version> <jstl-version>1.2</jstl-version> <taglibs-version>1.1.2</taglibs-version> <project.build.sourceEncoding>UTF-8</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-8</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.1</version> </parent> <modelVersion>4.0.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.1</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>7070</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吧,
感謝關注「碼農星球」。本文版權屬於「碼農星球」。咱們提供諮詢和培訓服務,關於本文有任何困惑,請關注並聯系咱們。