新建maven ,选择org.apache.maven.archetypes maven-archetype-webapp.
项目中src/main/webapp/index.jsp报错,原因是我们没有导入ServletApi,需要早pom.xml中添加Servlet依赖
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 只在编译时和测试时运行-->
<scope>provided</scope>
</dependency>查看目录结构,发现只创建了src/main/resources,需要手动创建其他目录,点击java resources右键new->source folder,选择项目名,输入src/main/java 不能创建src/main/java,在build path中删除src/main/java和src/test/java,然后在新建这两个目录(或者切换到navigator视图创建也可以),
然后查看build path 确保Source输出目录都是webdemo/target/class。
点击项目右键->properties ,在Deployment Assemble中,将/src/test/java、/src/test/resources删掉,因为他们都部署到了WEB-INF/class目录中了 。
pom.xml中添加tomcat插件:
<build>
<finalName>testweb2</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
运行maven run configuration ->tomcat:run 这样项目就启动了<finalName>testweb2</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>