詳解使用Maven開發(fā)Web應(yīng)用詳細(xì)步驟
開發(fā) Web 應(yīng)用的思路
實(shí)現(xiàn)一個(gè)簡單的 JSP/Servlet。
- 搭建創(chuàng)建 Web 應(yīng)用工程的環(huán)境。
- 創(chuàng)建 Web 應(yīng)用工程。
- Web 應(yīng)用工程的目錄結(jié)構(gòu)。
- 結(jié)合 Web 服務(wù)器,發(fā)布 Web 應(yīng)用。
- 體驗(yàn) Web 應(yīng)用的開發(fā)和發(fā)布測(cè)試過程。
實(shí)現(xiàn)經(jīng)典的 MVC 版本的用戶 CRUD。
- 熟練第 1 步中的幾個(gè)方面。
- 結(jié)合典型的業(yè)務(wù)邏輯,實(shí)現(xiàn) CRUD。
實(shí)現(xiàn) Web 版 HelloWorld
1)選擇 File→New→Others 命令。選擇 Create Maven Project 命令,單擊“下一步”按鈕。選中創(chuàng)建 Web 應(yīng)用工程的 Archetype,如圖 1 所示。

也可以選擇其他類似的,創(chuàng)建 Web 應(yīng)用的都可以,比如 maven-archetype-webapp 也可以。當(dāng)然,也可以選擇從網(wǎng)上找到坐標(biāo)后的 Archetype 插件,再安裝進(jìn)去。
怎么安裝新的 Archetype 呢?單擊圖中的 Add Archetype… 按鈕,在出現(xiàn)的窗口中輸入在網(wǎng)上找到的插件坐標(biāo)信息,如圖 2 所示。

單擊 OK 按鈕,MyEclipse 會(huì)自動(dòng)下載該構(gòu)件。重新打開創(chuàng)建工程的向?qū)ы撁?,就可以發(fā)現(xiàn)新增了剛剛添加的 Archetype 插件,如圖 3 所示。

2)點(diǎn)擊“next”,在下一個(gè)界面中輸入新創(chuàng)建的 Web 工程的坐標(biāo)信息和包名,如圖 4 所示。

3)單擊 Finish 按鈕,M2Eclipse 會(huì)自動(dòng)創(chuàng)建一個(gè) Web 工程 MvnDemo02。其在 src/main 目錄下添加了 webapp 目錄,里面有 Web 應(yīng)用特有的 WEB-INF 目錄,web.xml 和 index.jsp 等。
其中,webapp 目錄和里面的文件以及結(jié)構(gòu)在 Maven 中也是固定的。這樣就創(chuàng)建好了 Web 應(yīng)用工程。
編寫樣例代碼
工程創(chuàng)建好了,下一步就是寫測(cè)試代碼了。接下來會(huì)寫 3 個(gè)代碼(2 個(gè) jsp 和 1 個(gè)servlet)。
index.jsp,里面顯示輸入框,能提交輸入的內(nèi)容,代碼如下所示:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index JSP</title>
</head>
<body>
<form action="welcomeServlet" method="post">
請(qǐng)輸入問候人名:<input type='text' name="name"/><br/>
<input type='submit' value='問候'/>
</form>
</body>
</html>
welcome.jsp,顯示問候信息,代碼如下所示:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome JSP</title>
</head>
<body>
問候信息:${welcome }
</body>
</html>
welcomeServlet,接收 index.jsp 發(fā)過來的名稱,生成問候信息,轉(zhuǎn)給 welcome.jsp 顯示。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class WelcomeServlet
*/
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String welcome = "Hello," + name;
request.setAttribute("welcome", welcome);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
當(dāng)然,除了編寫代碼外,還需要配置 web.xml,servlet 的,web.xml 代碼如下所示:
<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"
version="2.5">
<display-name>MvnDemo02</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>WelcomeServlet</display-name>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>com.mengma.demo.MvnDemo02.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
構(gòu)建 Web 項(xiàng)目
前期的構(gòu)建過程同前面基本的 Java 工程一樣,根據(jù)自己的需要,在 pom.xml 中配置好對(duì)應(yīng)功能的插件,再運(yùn)行對(duì)應(yīng)的圖形化菜單命令就可以了,在這里不做重復(fù)說明。
一個(gè) Web 應(yīng)用構(gòu)建好后,不只是編譯打包安裝就可以了,還需要將它發(fā)布到 Web 服務(wù)器中進(jìn)行測(cè)試調(diào)試才行。這里主要介紹兩種發(fā)布到 Tomcat 7 服務(wù)器啟動(dòng)測(cè)試的方式。在項(xiàng)目開發(fā)過程中可以根據(jù)自己的需要,選擇其中一種。
1. 使用 Maven 的 Jetty 插件部署 Web
在 pom.xml 中添加 Jetty 插件的坐標(biāo)信息,內(nèi)容如下:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<webAppSourceDirectory>${basedir}/src/main/webapp</webAppSourceDirectory>
</configuration>
</plugin>
在 MyEclipse 中配置 Web 服務(wù)器運(yùn)行環(huán)境。
選擇 MyEclipse 菜單 Window→Preferences 命令,打開 Preferences 窗口,選中左邊樹 Server→Runtime Environment,如圖 5 所示。

單擊右邊的 Add… 按鈕,彈出一個(gè)選擇服務(wù)器的窗口。選中窗口中的 Apache Tomcat v 7.0 服務(wù)器,如圖 6 所示。

單擊 Next 按鈕,進(jìn)入選擇 Tomat Server 配置頁面,選擇 Tomcat 的安裝目錄和 JRE 運(yùn)行環(huán)境(JDK),如圖 7 所示。

單擊 Finish 和 Apply and Close 按鈕,關(guān)閉所有配置窗口,完成 MyEclipse 中的 Web Server 配置。
右擊“工程”,選擇 Run As→Maven build 命令,打開自定義 launch 窗口,在 Goals 中輸入啟動(dòng)的插件名和目標(biāo)“jetty:run”,如圖 8 所示。

單擊 Run 按鈕運(yùn)行一次后,以后每次都可以在 Run As→Maven build 命令中選擇重復(fù)運(yùn)行。
服務(wù)器啟動(dòng)了,接下來打開瀏覽器,輸入:
http://localhost:8080/MvnDemo02/index.jsp
這樣就可以訪問第一個(gè)頁面了。
2. 使用 cargo-maven2-plugin 插件部署 Web
使用 cargo 插件相對(duì)簡單,只需在 pom.xml 中進(jìn)行配置,指定部署應(yīng)用所需要的信息,再運(yùn)行 Run As→Maven install 命令,cargo 插件自動(dòng)會(huì)把打成 war 包的應(yīng)用,發(fā)布到指定 Web 服務(wù)器的發(fā)布目錄下。
接下來要做的是啟動(dòng) Web 服務(wù)器,按以前的方式打開瀏覽器瀏覽頁面。
Gargo 在 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>cn.com.mvnbook.demo</groupId>
<artifactId>MvnDemo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>MvnDemo02 Web App</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<webAppSourceDirectory>${basedir}/src/main/webapp</webAppSourceDirectory>
</configuration>
</plugin>
<plugin>
<!-- 指定插件名稱及版本號(hào) -->
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.8</version>
<configuration>
<!--是否說明,操作start、stop等后續(xù)操作必須等前面操作完成才能繼續(xù) -->
<wait>true</wait>
<!-- 容器的配置 -->
<container>
<!-- 指定tomcat版本 -->
<containerId>tomcat7x</containerId>
<!-- 指定類型:standalone, installed等 -->
<type>installed</type>
<!-- 指定Tomcat的位置,即catalina.home -->
<home>C:\work\servers\apache-tomcat-7.0.69</home>
</container>
<!-- 具體的配置 -->
<configuration>
<!-- 類型,existing:存在 -->
<type>existing</type>
<!-- Tomcat的位置,即catalina.home -->
<home>C:\work\servers\apache-tomcat-7.0.69</home>
</configuration>
<deployables> <!-- 部署設(shè)置 -->
<deployable> <!-- 部署的War包名等 -->
<groupId>cn.com.mvnbook.demo</groupId>
<artifactId>MvnDemo02</artifactId>
<type>war</type>
<properties>
<context>MvnDemo02</context> <!-- 部署路徑 -->
</properties>
</deployable>
</deployables>
<deployer> <!-- 部署配置 -->
<type>installed</type> <!-- 類型 -->
</deployer>
</configuration>
<executions>
<!-- 執(zhí)行的動(dòng)作 -->
<execution>
<id>verify-deployer</id>
<phase>install</phase> <!-- 解析install -->
<goals>
<goal>deployer-deploy</goal>
</goals>
</execution>
<execution>
<id>clean-deployer</id>
<phase>clean</phase>
<goals>
<goal>deployer-undeploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>MvnDemo02</finalName>
</build>
</project>
右擊“工程”,選擇 Run As→Maven install 命令后,就可以在 Tomcat 7 的發(fā)布目錄下發(fā)現(xiàn) MvnDemo02.war,啟動(dòng)后它就能自動(dòng)發(fā)布并且能被訪問。
測(cè)試
不管前面哪種方式,啟動(dòng)服務(wù)器后,打開瀏覽器,輸入 http://localhost:8080/MvnDemo02/index.jsp 鏈接后,就可以進(jìn)行測(cè)試了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java開發(fā)之普通web項(xiàng)目轉(zhuǎn)為Maven項(xiàng)目的方法
- maven打包web項(xiàng)目時(shí)同時(shí)打包為war和jar文件的方法
- 使用Maven tomcat:run命令啟動(dòng)web項(xiàng)目時(shí)修改默認(rèn)端口的方法
- springmvc+maven搭建web項(xiàng)目
- 使用maven創(chuàng)建web項(xiàng)目的方法步驟(圖文)
- 微信開發(fā)準(zhǔn)備第一步 Maven倉庫管理新建WEB項(xiàng)目
- 使用IntelliJ IDEA 15和Maven創(chuàng)建Java Web項(xiàng)目(圖文)
- idea 創(chuàng)建 maven web 工程流程(圖文教程)
相關(guān)文章
Java業(yè)務(wù)中臺(tái)確保數(shù)據(jù)一致性的解決方案
數(shù)據(jù)一致性通常指關(guān)聯(lián)數(shù)據(jù)之間的邏輯關(guān)系是否正確和完整。而數(shù)據(jù)存儲(chǔ)的一致性模型則可以認(rèn)為是存儲(chǔ)系統(tǒng)和數(shù)據(jù)使用者之間的一種約定。如果使用者遵循這種約定,則可以得到系統(tǒng)所承諾的訪問結(jié)果2021-10-10
Java class文件格式之方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java class文件格式之方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringBoot中通過AOP整合日志文件的實(shí)現(xiàn)
本文主要介紹了SpringBoot中通過AOP整合日志文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Spring Boot示例分析講解自動(dòng)化裝配機(jī)制核心注解
這篇文章主要分析了Spring Boot 自動(dòng)化裝配機(jī)制核心注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-07-07
Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題
這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

