1.选择maven对应的webapp模板
2.捆绑对应的maven文件配置settings.xml和ext.(IDEA中有自带maven3,可以使用默认的maven3,但是在加载时会比较慢)
3.在项目中的main创建java包和rescources包,并对两个包进行对应的标记。
4.对应WEB-INF中的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"
metadata-complete="true">
</web-app>
5.在pom.xml导入所需要的依赖(每次导入一个新的依赖时都要通过maven的clean进行刷新)
6.对pom.xml中的 <build>的配置进行替换,使rescources中的xml文件和properties文件能被搜索到。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
7.在rescources中创建一个db.properties文件用于存储连接数据库所需配置的信息。
格式为下:
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/smbms?useSLL=false&useUnicode=true&characterEncoding=utf-8
username = root
password = pwd
8.如果要导入jar包就需要在WEB-INF中创建一个lib目录,用于存放jar包。
9.导入需使用的api和工具(通过'+'进行添加)。
10.配置tomcat的运行环境。
在部署中添加当前项目,这样才能通过本地服务器访问该项目。
最终完成JavaWeb的环境搭建。