image-20201213145127878
填写Module的项目信息,如下:
image-20201213145308747
配置 Maven 设置:
image-20201213145357863
配置Module的存储路径:
image-20201213145653588
image-20201213145746870
可以从目录结构来看,生成的目录结构缺少,需要手动配置一下工程目录。
创建 java 源码路径:
image-20201213150051596
创建 resources 配置文件夹:
image-20201213150227545
创建 test 单元测试文件夹:
image-20201213150357332
image-20201213150547615
image-20201213150845429
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 项目的坐标 -->
<groupId>com.lijw</groupId>
<artifactId>javaweb_demo_01</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 设置打包方式 -->
<packaging>war</packaging>
<!-- 项目名称以及服务url -->
<name>javaweb_demo_01 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!-- 项目属性,设置jdk 以及 编码 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 项目依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>javaweb_demo_01</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置 tomcat 部署 javaweb 工程,如下:
image-20201213151209191
image-20201213151252099
image-20201213151400738
image-20201213151454155
image-20201213151522869
使用骨架创建的 javaweb 工程,自动创建了 index.jsp 页面,我们可以测试访问:
image-20201213151722364
image-20201213151738704
image-20191224105135578
image-20191224105159688
image-20191224105359670
上面是使用骨架来创建工程的,如果不使用骨架,怎样创建工程呢?
image-20201213152328517
image-20201213152453490
image-20201213152604957
image-20201213152718813
<!-- 设置打包方式 -->
<packaging>war</packaging>
image-20201213154003927
image-20201213154135209
image-20201213154155981
image-20201213154246266
image-20201213154456585
image-20201213160920039
image-20201213154548360
image-20201213154619983
image-20201213154656116
image-20201213161030729
浏览器访问如下:
image-20201213154929582
首先搜索一下 Servlet 的 Maven 坐标,如下:
访问 https://mvnrepository.com/ 搜索 servlet 即可。
image-20201213155713092
将依赖拷贝到 pom.xml 中,如下:
image-20201213155759065
image-20201213155906346
image-20201213161312836
@WebServlet("/demo2")
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello java web demo 2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
image-20201213161332951
上面可以看到不使用骨架创建 javaweb 项目的话,还是需要自己手动去做一下目录结构的处理的。那么下面有一个插件可以帮助我们快速处理。
使用JBLJavaToWeb插件,可以轻松地将没有使用骨架创建的java项目转换成javaweb项目
image-20201214004331159
安装完成后会提示重启.
重启完成后可以直接使用该插件.
image-20201214004828333
image-20201214004928308
image-20201214004958829
image-20201214005023857
image-20201214005054384