亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解使用Maven開發(fā)Web應用詳細步驟

 更新時間:2019年11月13日 15:23:21   作者:吳聲子夜歌  
這篇文章主要介紹了詳解使用Maven開發(fā)Web應用詳細步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

開發(fā) Web 應用的思路

實現(xiàn)一個簡單的 JSP/Servlet。

  • 搭建創(chuàng)建 Web 應用工程的環(huán)境。
  • 創(chuàng)建 Web 應用工程。
  • Web 應用工程的目錄結構。
  • 結合 Web 服務器,發(fā)布 Web 應用。
  • 體驗 Web 應用的開發(fā)和發(fā)布測試過程。

實現(xiàn)經(jīng)典的 MVC 版本的用戶 CRUD。

  • 熟練第 1 步中的幾個方面。
  • 結合典型的業(yè)務邏輯,實現(xiàn) CRUD。

實現(xiàn) Web 版 HelloWorld

1)選擇 File→New→Others 命令。選擇 Create Maven Project 命令,單擊“下一步”按鈕。選中創(chuàng)建 Web 應用工程的 Archetype,如圖 1 所示。

圖 1 選擇Web Archetype

也可以選擇其他類似的,創(chuàng)建 Web 應用的都可以,比如 maven-archetype-webapp 也可以。當然,也可以選擇從網(wǎng)上找到坐標后的 Archetype 插件,再安裝進去。

怎么安裝新的 Archetype 呢?單擊圖中的 Add Archetype… 按鈕,在出現(xiàn)的窗口中輸入在網(wǎng)上找到的插件坐標信息,如圖 2 所示。

圖 2 添加 Archetype

單擊 OK 按鈕,MyEclipse 會自動下載該構件。重新打開創(chuàng)建工程的向導頁面,就可以發(fā)現(xiàn)新增了剛剛添加的 Archetype 插件,如圖 3 所示。

圖 3 選擇 webapp-javaee6 Archetype

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

圖 4 Maven項目坐標

3)單擊 Finish 按鈕,M2Eclipse 會自動創(chuàng)建一個 Web 工程 MvnDemo02。其在 src/main 目錄下添加了 webapp 目錄,里面有 Web 應用特有的 WEB-INF 目錄,web.xml 和 index.jsp 等。

其中,webapp 目錄和里面的文件以及結構在 Maven 中也是固定的。這樣就創(chuàng)建好了 Web 應用工程。

編寫樣例代碼

工程創(chuàng)建好了,下一步就是寫測試代碼了。接下來會寫 3 個代碼(2 個 jsp 和 1 個servlet)。

index.jsp,里面顯示輸入框,能提交輸入的內容,代碼如下所示:

<%@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ā)過來的名稱,生成問候信息,轉給 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);
  }
}

當然,除了編寫代碼外,還需要配置 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>

構建 Web 項目

前期的構建過程同前面基本的 Java 工程一樣,根據(jù)自己的需要,在 pom.xml 中配置好對應功能的插件,再運行對應的圖形化菜單命令就可以了,在這里不做重復說明。

一個 Web 應用構建好后,不只是編譯打包安裝就可以了,還需要將它發(fā)布到 Web 服務器中進行測試調試才行。這里主要介紹兩種發(fā)布到 Tomcat 7 服務器啟動測試的方式。在項目開發(fā)過程中可以根據(jù)自己的需要,選擇其中一種。

1. 使用 Maven 的 Jetty 插件部署 Web

在 pom.xml 中添加 Jetty 插件的坐標信息,內容如下:

<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 服務器運行環(huán)境。

選擇 MyEclipse 菜單 Window→Preferences 命令,打開 Preferences 窗口,選中左邊樹 Server→Runtime Environment,如圖 5 所示。

圖 5 MyEclipse的Web服務器

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

圖 6 添加 Tomcat 7.0

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

圖 7 添加 Tomcat 的 Java home

單擊 Finish 和 Apply and Close 按鈕,關閉所有配置窗口,完成 MyEclipse 中的 Web Server 配置。

右擊“工程”,選擇 Run As→Maven build 命令,打開自定義 launch 窗口,在 Goals 中輸入啟動的插件名和目標“jetty:run”,如圖 8 所示。

圖 8 運行 jetty

單擊 Run 按鈕運行一次后,以后每次都可以在 Run As→Maven build 命令中選擇重復運行。

服務器啟動了,接下來打開瀏覽器,輸入:

http://localhost:8080/MvnDemo02/index.jsp

這樣就可以訪問第一個頁面了。

2. 使用 cargo-maven2-plugin 插件部署 Web

使用 cargo 插件相對簡單,只需在 pom.xml 中進行配置,指定部署應用所需要的信息,再運行 Run As→Maven install 命令,cargo 插件自動會把打成 war 包的應用,發(fā)布到指定 Web 服務器的發(fā)布目錄下。

接下來要做的是啟動 Web 服務器,按以前的方式打開瀏覽器瀏覽頁面。

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>  <!-- 部署設置 -->
            <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ā)布并且能被訪問。

測試

不管前面哪種方式,啟動服務器后,打開瀏覽器,輸入 http://localhost:8080/MvnDemo02/index.jsp 鏈接后,就可以進行測試了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java業(yè)務中臺確保數(shù)據(jù)一致性的解決方案

    Java業(yè)務中臺確保數(shù)據(jù)一致性的解決方案

    數(shù)據(jù)一致性通常指關聯(lián)數(shù)據(jù)之間的邏輯關系是否正確和完整。而數(shù)據(jù)存儲的一致性模型則可以認為是存儲系統(tǒng)和數(shù)據(jù)使用者之間的一種約定。如果使用者遵循這種約定,則可以得到系統(tǒng)所承諾的訪問結果
    2021-10-10
  • Java面試題沖刺第二十五天--并發(fā)編程3

    Java面試題沖刺第二十五天--并發(fā)編程3

    這篇文章主要為大家分享了最有價值的三道關于并發(fā)編程的面試題,涵蓋內容全面,包括數(shù)據(jù)結構和算法相關的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 在Java中使用下劃線分隔數(shù)的字面值的用法講解

    在Java中使用下劃線分隔數(shù)的字面值的用法講解

    這篇文章主要介紹了在Java中使用下劃線分隔數(shù)字的字面值的用法講解,這是Java7以后加入的新特性,需要的朋友可以參考下
    2016-03-03
  • Java class文件格式之方法_動力節(jié)點Java學院整理

    Java class文件格式之方法_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java class文件格式之方法的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • SpringBoot中通過AOP整合日志文件的實現(xiàn)

    SpringBoot中通過AOP整合日志文件的實現(xiàn)

    本文主要介紹了SpringBoot中通過AOP整合日志文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Spring Boot示例分析講解自動化裝配機制核心注解

    Spring Boot示例分析講解自動化裝配機制核心注解

    這篇文章主要分析了Spring Boot 自動化裝配機制核心注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-07-07
  • springcloud組件技術分享(推薦)

    springcloud組件技術分享(推薦)

    Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發(fā)便利性簡化了分布式系統(tǒng)的開發(fā),比如服務發(fā)現(xiàn)、服務網(wǎng)關、服務路由、鏈路追蹤等。這篇文章主要介紹了springcloud組件技術分享,需要的朋友可以參考下
    2020-10-10
  • SpringBoot 自動裝配的原理詳解分析

    SpringBoot 自動裝配的原理詳解分析

    這篇文章主要介紹了SpringBoot 自動裝配的原理詳解分析,文章通過通過一個案例來看一下自動裝配的效果展開詳情,感興趣的小伙伴可以參考一下
    2022-08-08
  • 淺談spring 常用注解

    淺談spring 常用注解

    這篇文章主要介紹了淺談spring 常用注解,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Maven?Pom?文件中的隱式依賴導致Jar沖突問題

    Maven?Pom?文件中的隱式依賴導致Jar沖突問題

    這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導致Jar沖突問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論