SpringMvc
Spring MVC 是 Spring 框架的一部分,它是一个设计用来构建 web 应用的框架。Spring MVC 实现了 Model-View-Controller(MVC)设计模式,帮助开发者将业务逻辑、用户界面以及输入处理等职责分离,从而提高代码的可维护性和可测试性。
1 创建maven工程
1)打开 IntelliJ IDEA:
启动 IntelliJ IDEA。

2)选择新建项目:
在欢迎界面选择 " New Project"。

3)选择 Maven:
在项目类型列表中找到并选择 "Maven" >"。

4)设置 Group Id 和 Artifact Id:
- 在 "Group Id" 字段输入你的项目的唯一标识符,通常是一个反向域名。
- 在 "Artifact Id" 字段输入项目的名称。
- 如果需要的话,可以修改项目的安装目录位置。

5)选择 Archetype:
- 选择一个合适的 Archetype。Archetype 是一个模板,它可以帮助你快速创建具有特定结构的项目。对于简单的 Java 应用,可以选择 "maven-archetype-quickstart" 或者 "maven-archetype-j2ee-simple"。
- 如果没有看到你需要的 Archetype,可以点击 "Browse Repositories..." 下载更多。

6)配置 Archetype 参数
- 根据所选 Archetype 的要求填写参数。例如,如果是 "maven-archetype-quickstart",可能只需要确认默认的选项即可。
- 点击 "Finish" 完成项目创建。

2 修改打包方式为war
要将 Maven 项目的打包方式修改为 war 包,你需要编辑项目的 pom.xml 文件,并确保 <packaging> 元素被设置为 war。
打开 pom.xml 文件:
- IDE 打开你的项目的 pom.xml 文件。
定位 <packaging> 标签:
- 寻找 <packaging> 标签。如果 <packaging> 标签不存在,默认情况下 Maven 会假设这是一个 jar 类型的项目。
修改 <packaging> 标签:
- 将 <packaging> 标签设置为 war。如果标签不存在,则需要添加它。
3 导入依赖
注:这个是在父工程导入的,如果子工程继承父工程则不需要在导入了
如果不在父工程的pom.xml文件导入,就需要在子工程的pom.xml文件中导入以下依赖
<!--springMvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.12</version>
</dependency>
<!--logback-classic日志--><dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.7</version>
</dependency>
<!--servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
</dependency>
<!--thymeleaf-spring6-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring6 </artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

注:记得刷新maven
idea===>右边栏 ===》maven ===》新界面===>maven刷新
4.新建module
- 修改模块名字
- 选择开发语言
- 选择Build system
- 选择JDK
- 编写Groupid
- 编写ArtifactId
- 点击create按钮

5.修改module的模块
1.找到新建module模块的pom.xml
project ===> mvc-01 ===> src ==> pom.xml

2.修改打包方式为war
<?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> <parent> <groupId>org.xiji</groupId> <artifactId>spring-mvc</artifactId> <version>1.0-SNAPSHOT</version> </parent> \<packaging\>war\</packaging\> <artifactId>mvc-01</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>

3.刷新maven工程
idea ===》右边maven图标 ===》刷新按钮 ==》Maven刷新


6.新建webapp文件夹
project ==> mvc-01 ==>src===>main ===>webapp

7.找到Project Structure
File ===> Project Structure

8.找到module中的web
Moudule ===> mvc-01 ===> web

9.添加xml文件
- 点击+号
- 点击web.xml
- 在路径中添加如下src\main\webapp
- 点击ok
- 点击apply
- 点击ok


生成如下内容

10. 创建springConfig.xml文件
在resources文件中创建springMvc.xml

11.编写springConfig.xml文件
开启包扫描
开启springMvc注解驱动
配置Thymeleaf
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启包扫描-->
<context:component-scan base-package="org.xiji"/>
<!--开启springmvc注解驱动-->
<mvc:annotation-driven/>
<!--配置设置视图-->
<bean id="viewResolverRegistry" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"></property>
</bean>
<!--配置引擎-->
<bean id="templateEngine" class="org.thymeleaf.spring6.SpringTemplateEngine">
<property name="templateResolver" ref="resolver"></property>
</bean>
<!--配置模板解析器-->
<bean id="resolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="cacheable" value="false"></property>
</bean>
</beans>
12.编写web.xml
- 配置DispatcherServlet
- 配置名
- 配置类
- 配置项目的springConfig文件
- 提前加载SpringConfig文件
<?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"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> \<!--配置项目的springConfig配置文件--\> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springConfig.xml</param-value> </init-param> \<!--DispatcherServlet启动时加载springConfig配置文件--\> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

13.为项目配置tomcat
Current File ==>Edit Configuration

点击+号 ====》 找到tomcat ===> local
点击tomcat ===>Name (配置名字) ===》Configure ===>点击文件夹 ===》找到Tomcat安装跟目录

点击fix按钮

选择第一个,或者第二个

注: Application context 为项目访问路径



14.创建index.html文件
在resources 文件夹下的templates文件夹下创建
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>欢迎来到我的网站</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f9; } header { background-color: #333; color: white; padding: 10px 20px; text-align: center; } nav { background-color: #444; color: white; padding: 10px 20px; text-align: center; } nav a { color: white; text-decoration: none; margin: 0 10px; } main { padding: 20px; text-align: center; } footer { background-color: #333; color: white; padding: 10px 20px; text-align: center; } </style> </head> <body> <header> <h1>welcome to my website</h1> </header> <nav> <a href="#">index</a> <a href="#">about </a> <a href="#">call me</a> </nav> <main> <h2>this is my first web page</h2> <p>2024/9/11</p> </main> <footer> @2024 my website </footer> </body> </html>

15.创建controller
- @Controller注解 标注控制层
- @Service注解 标注服务层
- @Repository 标注Dao层
- @Resource 自动注入
- @Autowired 自动注入
package org.xiji.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class MyController{ @RequestMapping("/index") public String index(){ return "index"; } }
16.启动tomcat

访问:127.0.0.1:8080/mvc/index

附加:
Tomcat配置
如果tomcat不会配置,下面是我以前写的Tomcat配置,还有下载
Tomcat无法启动
如果碰到Tomcat无法启动等问题,可以看下面这一篇