本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1、新建 maven 项目 ,使用模型(勾选create archetype) ,选择创建 webapp项目(选择 maven - archetype - webapp)
2、输入 groupId(组织名称 一般是公司域名倒写) 和 ArtifactId (项目名称)
3、选择 maven home directory 和 user settings file
把 user settings file 设为 maven安装目录里的conf 里的setting文件,里面的文件具体要不要改等下次用到了再记录
ps:可以在file--》other settings--》第一个setting(默认设置)里---》设置 maven路径和setting.xml文件路径,这样以后创建这个路径就都是这个路径,方便。
4、完成
5、选 自动导入 : Enable Auto-Import
6、等待 下载完成 : Maven execution finished
7、在 pom.xml 文件中添加依赖
ps:这个依赖有一个专门的中央库保存了,谷歌搜索 maven repository 就OK(需翻墙)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 编译时使用,部署之后不用-->
<scope>provided</scope>
</dependency>
<!-- 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.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl\</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
放在 dependencies 标签中( 如果 有红色的 字体证明没有下载完毕,请消停等待,别BB )
8、将 <pluginManagement></pluginManagement> 元素 删除
9、添加 对应的插件 :
<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>\
\<compilerVersion>1.8\</compilerVersion>\
\<encoding>UTF-8\</encoding>\
\</configuration>\
\</plugin>
\<!-- 添加 Tomcat 7 支持-->\
\<plugin>\
\<groupId>org.apache.tomcat.maven\</groupId>\
\<artifactId>tomcat7-maven-plugin\</artifactId>\
\<version>2.2\</version>\
\<configuration>\
\<port>8080\</port>\
\<path>/\</path>\
\<uriEncoding>UTF-8\</uriEncoding>\
\<useBodyEncodingForURI>true\</useBodyEncodingForURI>\
\</configuration>\
\</plugin>\
\<!-- 添加 Jetty 支持-->\
\<plugin>\
\<groupId>org.eclipse.jetty\</groupId>\
\<artifactId>jetty-maven-plugin\</artifactId>\
\<version>9.4.8.v20171121\</version>\
\<configuration>\
\<httpConnector>\
\<port>8080\</port> \<!-- 这里指定 jetty 服务的端口号 -->\
\</httpConnector>\
\<useTestClasspath>true\</useTestClasspath>\
\<webAppConfig>\
\<contextPath>/\</contextPath> \<!-- 这里指定在浏览器访问时,当前Web应用的根路径 -->\
\</webAppConfig>\
\</configuration>\
\</plugin>\
\</plugins>
如果 在 右侧的 maven project 中看到了红线,证明下载有问题 ,请 点击带有 向下箭头的 名字叫 download ...的一个图标
10 、测试 : 点开 右侧的 maven project ,点击 一个交 Plugins 的 下拉东西 ,如果下载完毕,内部会有一个交 jetty 和 tomcat7的插件 。
点开 jetty ,双击 jetty:run 的 一个 命令 (可能会进行下载,出现error 证明失败 )。出现 Started Jetty Server表示成功
点开 tomcat7 , 双击 tomcat7:run 的命令,可能会出现下载,出现error 证明失败。出现 Started Tomcat Server表示成功
注意:这两个命令不能同时进行 ,会出现端口冲突问题 。
11、在浏览器中 输入 localhost:8080 , 能看到 Hello World! 证明成功
12、在 目录结构中,选中 src ,右键,新建文件夹,名字 test 。选中 test ,右键,有一个叫 Mark Directory as ---> Test Source root
13、在 目录结构中,右键 main ,新建文件夹,名字 java , 选中 java 右键,有一个叫 Mark Directory as ---> Sources Root
以后的Java 代码放在哪里
14、在 目录结构中,右键 main ,新建文件夹,名字 resources , 选中 resources 右键,有一个叫 Mark Directory as ---> Resources Root
以后的配置文件,放在哪里
15、在 目录结构中,找到 web.xml , 将以下内容替换 \
\<?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">\
\</web-app>