eclipse Maven创建 web项目

时间:2022-12-29 15:58:15
  1. 转到 New 菜单 Other.. -> Maven -> Maven Project ,然后单击 Next
    eclipse Maven创建 web项目

  2. 选择Create a simple project并指定项目保存的目录,然后单击Next
    eclipse Maven创建 web项目
  3. 填写项目信息,然后单击Finish
    eclipse Maven创建 web项目
  4. 打开pom.xml文件并在其中添加servlet依赖项和Tomcat maven插件
    <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.mycompany.test</groupId>
      <artifactId>webproject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>webproject</name>
      <description>maven-web-project</description>
      
       <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
      </properties>
    
    
        <dependencies>
        <!-- Servlet API -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    
    
      <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
    
    
          <!-- Embedded Apache Tomcat required for testing war -->
    
    
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <path>/</path>
            </configuration>
          </plugin>
        </plugins>
      </build>
      
      
    </project>
  5. 项目结构
    eclipse Maven创建 web项目
  6. 在src/main/webapp中添加WEB-INF文件夹,在文件夹中添加web.xml文件
    eclipse Maven创建 web项目

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>webproject</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
      <servlet-name>hello</servlet-name>
      <servlet-class>com.mycompany.test.HelloWorldServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>hello</servlet-name>
      <url-pattern>/*</url-pattern>
      </servlet-mapping>
      
    </web-app>
  7. 添加测试类

    package com.mycompany.test;


    import java.io.IOException;


    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class HelloWorldServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;


       @Override
       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           resp.setContentType("text/plain");
           resp.getWriter().write("Hello World! Maven Web Project Example.");
       }
    }
  8. 运行项目
    访问   http://localhost:8080/webproject/    
        <version>2.2</version>        <configuration>          <path>/</path>        </configuration>