IDEA创建Maven Web项目
概述
以IDEA2020.3.1+Tomcat9.0+Maven3.6.3+jdk1.8+web4.0演示,单项目
Maven Mac下安装和IDEA配置见 https://www.cnblogs.com/shenleg/p/14218613.html
新建项目
IDEA补目录
直接选择
更改Web4.0版本
早期的web工程中并不支持@WebServlet
注解配置,甚至不支持El
表达式(在web 3.0版本之后才支持)
一定要将matadata-complete
属性显式改为"false"
这样创建的web工程才是web-4.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="false">
</web-app>
修改创建模版
手动修改默认模版,默认为1.4
版本
位置在:%maven_repository%\org\apache\maven\archetypes\maven-archetype-webapp\1.4
更改Pom文件
防止资源导入错误
<build>
<!-- 防止资源导入错误 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
引入Servlet API
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
引入后可以右键新建Servlet
文件了
引入Servlet JSP
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
引入JSTL API
部分Tomcat需要引入jstl的包,否则会报错:JSTL解析错误
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
JSTL依赖包含了jsp-api和servlet-pai:
太低servlet-api不支持@WebServlet
注解
引入Strand
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
引入MySQL驱动包
mysql 5.7 用8.0版本的驱动可以,5.1版本也可以,MySQL 5.5, 5.6, 5.7, and 8.0(都兼容,大胆用了)
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
引入后记得手动更新一下Maven
指定Maven JDK编译版本
pom中修改
局部修改
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Maven修改
全局修改,在maven配置文件settings.xml
中设置
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
pom补充
maven.compiler.source
和maven.compiler.target
仅仅是推荐,不是强制
指定项目JDK版本
模块也可以单独指定:
装Tomcat9.0
运行测试
重新部署
本文来自博客园,作者:神乐g,转载请注明原文链接:https://www.cnblogs.com/shenleg/p/14251101.html