踩过的雷
1.将普通javaweb项目改为maven webapp时不能先转移文件再配置pom.xml中依赖,这样会引起很多奇奇怪怪的麻烦,得先配置好所有的依赖和插件
2.el表达式 如${pageContext.request.contextPath}在web.xml 2.0版本下不能识别,得手动跟换版本,IDEA自动给你配置的版本可能是2.0会,所有得手动在web.xml里更改,这里提供一下整一个web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID"
version="3.0">
</web-app>
3.在doGet连接网页获取中文消息头获得乱码,比如http://localhost:9090/mavenssm/user/showUser?id=中国,获取id得到乱码,在普通javaweb项目不会有这个问题,因为tomcat8版本以上,服务器自动配置字符集为utf-8,但是在maven里pom.xml的tomcat插件里你要去配置,不然会出现乱码问题,整个tomcat配置代码在下面,把它添加进pom.xml里面的< plugins></ plugins >里面,tomcat7:run运行项目
<plugin>
<groupId>org.apache.tomcat.maven
</groupId>
<artifactId>tomcat7-maven-plugin
</artifactId>
<version>2.2
</version>
<configuration>
<port>8888
</port>
<uriEncoding>UTF-8
</uriEncoding>
<path>/
</path>
</configuration>
</plugin>
4.在maven项目控制台System.out.println出乱码是没有设置VM Options 参数为-DarchetypeCatalog=internal -Dfile.encoding=GBK
-DarchetypeCatalog=internal 是加快项目创建,每次创建项目时, IDEA 要使用插件进行创建,这些插件当你创建新的项目时,它每次都会去中央仓库下载,这样使得创建比较慢。应该创建时,让它找本地仓库中的插件进行创建项目
-Dfile.encoding=GBK 是在本地编译(Java 源代码-> Java 字节码 阶段)默认采用了GBK字符集,解决打印在控制台时乱码问题
5.连接中央仓库下载时不使用镜像会选择得特别慢,使用镜像也很简单,就在你下载maven-conf-setting.xml里面添加一段代码就行,比如我的路径是D:\apache-maven-3.5.2\conf\setting.xml 在这个xml 的< mirrors >< /mirrors >里面增加一段代码,就可以连接阿里云的镜像了
<mirror>
<id>nexus-aliyun
</id>
<mirrorOf>*
</mirrorOf>
<name>Nexus aliyun
</name>
<url>http://maven.aliyun.com/nexus/content/groups/public
</url>
</mirror>
6.maven webapp工程的目录结构,这里很重要,比如你要是把,css,js,img文件放错了,放到WEB-INF下,读取这些文件都还要获得权限
7.同一资源文件(如properties,txt文件)在个maven项目服务器里面resource下的资源路径与普通的javaweb项目在src路径下下有细微差别,
maven下 /D:/maven-java/bigProject/target/classes/druid.properties普通Javaweb项目 /D:/java_ee/day17_case/out/production/day17_case_war_exploded/druid.properties
8.配置依赖< dependency>会很麻烦的,比如jdbc的配置有配置多个依赖,比如有些会有版本问题,所以的多小心,这里提供一下一些我用过没有问题的依赖
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.12
</version>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter
</groupId>
<artifactId>junit-jupiter
</artifactId>
<version>RELEASE
</version>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>javax.servlet-api
</artifactId>
<version>3.1.0
</version>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp
</groupId>
<artifactId>jsp-api
</artifactId>
<version>2.0
</version>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-context
</artifactId>
<version>5.0.8.RELEASE
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-jdbc
</artifactId>
<version>5.0.8.RELEASE
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>8.0.16
</version>
</dependency>
<dependency>
<groupId>commons-dbcp
</groupId>
<artifactId>commons-dbcp
</artifactId>
<version>1.4
</version>
</dependency>
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid
</artifactId>
<version>1.1.21
</version>
</dependency>
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid
</artifactId>
<version>1.1.15
</version>
</dependency>
<dependency>
<groupId>taglibs
</groupId>
<artifactId>standard
</artifactId>
<version>1.1.2
</version>
</dependency>
<dependency>
<groupId>jstl
</groupId>
<artifactId>jstl
</artifactId>
<version>1.2
</version>
</dependency>
<dependency>
<groupId>commons-beanutils
</groupId>
<artifactId>commons-beanutils
</artifactId>
<version>1.9.3
</version>
</dependency>
我的IDEA版本是2019.3.3 tomcat是apache-tomcat-8.5.31 maven是apache-maven-3.5.2,版本有太大差别有时以上建议不一定适用
还有很多小细节和IDEA快捷操作我没有一一打出来,有时间会补充的
希望看完觉得有帮助的点个赞鼓励一下吧