使用Maven怎么開發(fā)一個Web應(yīng)用?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是一家專業(yè)提供阿勒泰企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、H5建站、小程序制作等業(yè)務(wù)。10年已為阿勒泰眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
開發(fā) Web 應(yīng)用的思路
實(shí)現(xiàn)一個簡單的 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ā)布測試過程。
實(shí)現(xiàn)經(jīng)典的 MVC 版本的用戶 CRUD。
熟練第 1 步中的幾個方面。
結(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 會自動下載該構(gòu)件。重新打開創(chuàng)建工程的向?qū)ы撁?,就可以發(fā)現(xiàn)新增了剛剛添加的 Archetype 插件,如圖 3 所示。
2)點(diǎn)擊“next”,在下一個界面中輸入新創(chuàng)建的 Web 工程的坐標(biāo)信息和包名,如圖 4 所示。
3)單擊 Finish 按鈕,M2Eclipse 會自動創(chuàng)建一個 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)建好了,下一步就是寫測試代碼了。接下來會寫 3 個代碼(2 個 jsp 和 1 個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"> 請輸入問候人名:<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 中配置好對應(yīng)功能的插件,再運(yùn)行對應(yīng)的圖形化菜單命令就可以了,在這里不做重復(fù)說明。
一個 Web 應(yīng)用構(gòu)建好后,不只是編譯打包安裝就可以了,還需要將它發(fā)布到 Web 服務(wù)器中進(jìn)行測試調(diào)試才行。這里主要介紹兩種發(fā)布到 Tomcat 7 服務(wù)器啟動測試的方式。在項(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… 按鈕,彈出一個選擇服務(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 中輸入啟動的插件名和目標(biāo)“jetty:run”,如圖 8 所示。
單擊 Run 按鈕運(yùn)行一次后,以后每次都可以在 Run As→Maven build 命令中選擇重復(fù)運(yùn)行。
服務(wù)器啟動了,接下來打開瀏覽器,輸入:
http://localhost:8080/MvnDemo02/index.jsp
這樣就可以訪問第一個頁面了。
2. 使用 cargo-maven2-plugin 插件部署 Web
使用 cargo 插件相對簡單,只需在 pom.xml 中進(jìn)行配置,指定部署應(yīng)用所需要的信息,再運(yùn)行 Run As→Maven install 命令,cargo 插件自動會把打成 war 包的應(yīng)用,發(fā)布到指定 Web 服務(wù)器的發(fā)布目錄下。
接下來要做的是啟動 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> <!-- 指定插件名稱及版本號 --> <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í)行的動作 --> <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,啟動后它就能自動發(fā)布并且能被訪問。
關(guān)于使用Maven怎么開發(fā)一個Web應(yīng)用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
標(biāo)題名稱:使用Maven怎么開發(fā)一個Web應(yīng)用
本文來源:http://bm7419.com/article42/jcecec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、小程序開發(fā)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)