三、IDEA创建Maven项目
在创建项目之前我们了解一个简单的概念。
在 IntelliJ IDEA 中,项目(Project)也是一个独立的实体,但它更加灵活和细粒度。在 IntelliJ IDEA 中,一个项目可以包含多个模块(Module),每个模块可以有自己的源代码和设置。这种模块化的方式使得在 IntelliJ IDEA 中更容易管理大型项目。此外,IntelliJ IDEA 还引入了概念称为 "工作区"(Workspace),它类似于 Eclipse 的工作空间,用于组织和管理多个项目。
例如:
idea project ==>eclipse workspce
idea module==>eclipse project
1.配置Maven
1).设置编码集
2.输入File Encodings将编码改为UTF-8
3.配置maven安装路径
2.创建工作区
1). 配置JDK
2). 修改项目存放路径
这样我们的工作区间就创建好啦
3.创建module模块
1).在工作区间上右击➡Next➡Module
2).选择我们自己的JDK➡输入webapp后CTRL+↓选择maven-archetype-webapp➡Next
3).为module模块取名
4.配置Maven Property
小贴士:
archetypeCatalog用来指定maven-archetype-plugin读取archetype-catalog.xml文件的位置:
internal——maven-archetype-plugin内置的
local——本地的,位置为~/.m2/archetype-catalog.xml
remote——指向Maven中央仓库的Catalog
5.pom.xml配置依赖以及Maven插件
依赖字符串:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
Maven插件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin>
选择“手动挡”,不然你的代码有问题系统也会去下载依赖,本身代码就是错的下载也肯定失败,所以选择“手动挡”。
6.web.xml从2.3版本换成3.1
<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_3_1.xsd"
version="3.1">
7.编写Servlet和JSP页面测试
JSP页面
<%-- Created by IntelliJ IDEA. User: 索隆 Date: 2023/8/11 Time: 17:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> helllo idea我是jsp页面 </body> </html>
Servlet层
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author Java方文山 * @compay csdn_Java方文山 * @create 2023-08-11-17:46 */ @WebServlet("/idea") public class DemoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("hello idea我是后端Servlet"); req.getRequestDispatcher("/index.jsp").forward(req,resp); } }
7.配置Tomcat服务器
8.更改网络访问地址
运行结果:
到这里我的分享就结束了,欢迎到评论区探讨交流!!
如果觉得有用的话还请点个赞吧 ♥ ♥