Maven(二)——Maven管理簡單Web專案例子
阿新 • • 發佈:2019-02-01
以Tomcat(一)中的Restaurant專案為例,寫一個用Maven構建Java Web專案的例子。
建立一個資料夾:maven-restaurant
開啟CMD視窗,到maven-restaurant下,執行mvn archetype:generate命令,建立名為Restaurant的Java Web專案:
mvn archetype:generate -DgroupId=com.meteor -DartifactId=Restaurant -Dpackage=com.meteor -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype -webapp
- maven-restaurant下生成了Restaurant專案的目錄結構,如圖:
在main下,建立名為java的資料夾,把開發的java檔案放在這個目錄,這也是maven預設的約定。
- 修改pom.xml檔案
- 加入Servlet的依賴。
- 配置maven編譯外掛,maven-compiler-plugin
- Tomcat外掛,tomcat7-maven-plugn。
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.meteor</groupId>
<artifactId>Restaurant</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Restaurant Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Restaurant</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8181</port>
<uriEncoding>UTF-8</uriEncoding>
<finalName>Restaurant</finalName>
<server>tomcat</server>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 在java目錄下,建立包,名為com.meteor。
- 建立BraisedChickenServlet.java類
package com.meteor;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class BraisedChickenServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
String size = request.getParameter("size");
String spicy = request.getParameter("spicy");
if (size == null && size.equals("")) {
size = "small";
}
if (spicy == null && spicy.equals("")) {
spicy = "no spicy";
}
writer.println("<html><body>");
writer.println("<h1> give me a "+size+" and "+spicy+" chicken</h1>");
writer.println("</body></html>");
}
}
- 修改web.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">
<display-name>Restaurant</display-name>
<servlet>
<servlet-name>choose</servlet-name>
<servlet-class>
com.meteor.BraisedChickenServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>choose</servlet-name>
<url-pattern>/choose</url-pattern>
</servlet-mapping>
</web-app>
- 在webapp下建立BraisedChicken.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Restaurant-BraisedChicken</title>
</head>
<body>
<h1> 今天中午去吃黃燜雞米飯</h1>
</body>
</html>