【SpringMVC】十分钟跑起来一个SpringMVC项目
目录标题
- 1 项目概述
- 1.项目结构解析
- 2. MVC项目的结构和每个组件的作用:
- 3. 项目的工作流程:
- 4 后期可以扩展的点:
- 2.源码学习
- 1. HelloController 类,Spring MVC控制器
- 2 springmvc-servlet.xml - Spring MVC的主要配置文件
- 3.web 目录 - Web应用程序资源 WEB-INF/jsp - 存放JSP视图文件
- 3.1 hello.jsp
- 3.2 index.jsp
- 4,WEB-INF/web.xml - Web应用程序的部署描述文件
- 5. pom.xml - Maven项目的配置文件,用于管理项目依赖和构建过程
- 6. springmvc-03-annotation.iml - IntelliJ IDEA的项目模块文件
- 3 逐行学习代码
- 4 服务器设置 tomcat
1 项目概述
1.项目结构解析
项目在运行的时候 会自动生成 target文件夹
2. MVC项目的结构和每个组件的作用:
-
项目根目录:
springmvc-03-annotation
- 名称表明这是一个使用注解方式配置的Spring MVC项目
-
源代码目录:
src/main/java
nuc.ss.controller
包:HelloController
类:使用注解配置的控制器类- 控制器负责处理用户请求,进行业务逻辑处理,并返回视图
- 标注显示"100% methods"说明所有方法都已实现
-
资源配置目录:
src/main/resources
springmvc-servlet.xml
:Spring MVC的核心配置文件- 定义组件扫描
- 配置视图解析器
- 配置静态资源处理
- 配置注解驱动等
-
Web应用目录:
web
WEB-INF/
- 安全目录,外部无法直接访问jsp/
目录:存放JSP视图文件hello.jsp
- 展示"Hello World"的视图index.jsp
- 应用的首页
web.xml
- Web应用的部署描述文件- 配置DispatcherServlet
- 配置URL映射
- 配置字符编码过滤器等
-
项目构建文件:
pom.xml
:Maven项目配置文件- 定义项目坐标(groupId, artifactId, version)
- 管理项目依赖(Spring MVC, Servlet, JSP等)
- 配置构建插件
- 管理项目生命周期
-
target/
目录:- 存放编译后的类文件和资源
- 包含最终的WAR包
-
IDE相关文件:
springmvc-03-annotation.iml
:IntelliJ IDEA的项目模块文件- 包含项目配置信息和依赖关系
3. 项目的工作流程:
- 用户发送请求到服务器
- 请求被DispatcherServlet接收
- 根据URL映射到对应的Controller方法
- Controller处理请求并返回视图名称
- 视图解析器找到对应的JSP文件
- 渲染视图并返回给用户
4 后期可以扩展的点:
- 可以添加Service层处理业务逻辑
- 可以添加DAO层处理数据访问
- 考虑添加日志配置文件
- 可以添加测试目录进行单元测试
- 可以考虑使用更现代的视图技术(如Thymeleaf)替代JSP
这个项目结构是典型的Spring MVC应用,适合学习Spring MVC的基础知识和注解配置方式。
2.源码学习
src/main/java - 存放Java源代码
1. HelloController 类,Spring MVC控制器
package nuc.ss.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/h1")
public String hello(Model model) {
//封装数据
model.addAttribute("msg","Hello,这是最简单的SpringMVC!!!");
return "hello";//会被视图解析器处理
}
}
src/main/resources - 存放配置文件
2 springmvc-servlet.xml - Spring MVC的主要配置文件
springmvc-servlet.xml
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="nuc.ss.controller"/>
<!-- 让Spring MVC不处理静态资源(css,js) -->
<mvc:default-servlet-handler />
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping注解来完成映射关系
要想使@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.web 目录 - Web应用程序资源 WEB-INF/jsp - 存放JSP视图文件
3.1 hello.jsp
<%--<%–--%>
<%-- Created by IntelliJ IDEA.--%>
<%-- User: Administrator--%>
<%-- Date: 2020/7/13 0013--%>
<%-- Time: 14:26--%>
<%-- To change this template use File | Settings | File Templates.--%>
<%--–%>--%>
<%--<%@ page contentType="text/html;charset=UTF-8" language="java" %>--%>
<%--<html>--%>
<%--<head>--%>
<%-- <title>Title</title>--%>
<%--</head>--%>
<%--<body>--%>
<%--${msg}--%>
<%--</body>--%>
<%--</html>--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Spring MVC Demo</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card {
background: yellowgreen;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
text-align: center;
}
.message {
color: #cb2525;
font-size: 24px;
}
</style>
</head>
<body>
<div class="card">
<div class="message">
${msg}
</div>
</div>
</body>
</html>
3.2 index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/7/13 0013
Time: 14:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>
4,WEB-INF/web.xml - Web应用程序的部署描述文件
<?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">
<!--1.注册servlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会被springmvc拦截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. pom.xml - Maven项目的配置文件,用于管理项目依赖和构建过程
<?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">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>nuc.ss</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springmvc-03-annotation</artifactId>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
6. springmvc-03-annotation.iml - IntelliJ IDEA的项目模块文件
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
</module>
这是一个使用注解配置方式的Spring MVC示例项目,从项目名称 springmvc-03-annotation 可以看出这可能是一个学习或练习项目。项目使用Maven进行依赖管理,并采用了JSP作为视图技术。
3 逐行学习代码
我来帮您详细注释每个关键代码文件:
- HelloController.java:控制器类
package nuc.ss.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller // 标识这是一个Spring MVC控制器
@RequestMapping("/hello") // 类级别的请求映射,指定这个控制器的基础URL路径
public class HelloController {
@RequestMapping("/h1") // 方法级别的请求映射,完整访问路径为/hello/h1
public String hello(Model model) { // Model用于向视图传递数据
// 向模型中添加属性,key为"msg",value为显示的消息
// 这个数据可以在视图(JSP)中通过${msg}获取
model.addAttribute("msg", "Hello,这是最简单的SpringMVC!!!");
// 返回视图名称,会被视图解析器处理
// 根据配置会在返回的字符串前加上前缀/WEB-INF/jsp/,后加上后缀.jsp
return "hello";
}
}
- springmvc-servlet.xml:Spring MVC配置文件
<?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="..."> <!-- 这里是XML Schema位置定义 -->
<!-- 启用注解扫描,自动扫描包下的所有注解 -->
<!-- 会扫描指定包下的@Controller, @Service, @Repository, @Component等注解 -->
<context:component-scan base-package="nuc.ss.controller"/>
<!-- 配置静态资源处理,使Spring MVC不处理静态资源 -->
<!-- 例如.js,.css等文件会交由默认的servlet处理 -->
<mvc:default-servlet-handler />
<!-- 启用Spring MVC注解驱动 -->
<!-- 自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!-- 支持@Controller、@RequestMapping等注解 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 配置视图路径前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 配置视图路径后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
- web.xml:Web应用配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0">
<!-- 配置Spring MVC的前端控制器 -->
<servlet>
<!-- servlet的名称 -->
<servlet-name>SpringMVC</servlet-name>
<!-- DispatcherServlet是Spring MVC的核心,负责接收请求并分发 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring MVC配置文件的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定配置文件位置 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 配置servlet启动顺序,数字越小优先级越高 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置servlet映射 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- /表示拦截所有请求,不包括.jsp -->
<!-- /*表示拦截所有请求,包括.jsp -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- hello.jsp:视图文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Spring MVC Demo</title>
<style>
/* 页面样式设置 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f2f5;
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
min-height: 100vh;
}
.card {
background: yellowgreen; /* 卡片背景色 */
padding: 30px;
border-radius: 8px; /* 圆角边框 */
box-shadow: 0 2px 4px rgba(0,0,0,0.1); /* 阴影效果 */
text-align: center;
}
.message {
color: #cb2525; /* 文字颜色 */
font-size: 24px;
}
</style>
</head>
<body>
<div class="card">
<div class="message">
${msg} <!-- 显示从控制器传来的消息 -->
</div>
</div>
</body>
</html>
- index.jsp 的详细注释
jspCopy<%--
Created by IntelliJ IDEA. // IDE创建信息
User: Administrator // 创建用户
Date: 2020/7/13 0013 // 创建日期
Time: 14:21 // 创建时间
To change this template use File | Settings | File Templates. // 模板修改提示
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%-- 页面指令:设置内容类型和编码 --%>
<html>
<head>
<title>$Title$</title> <%-- 页面标题占位符 --%>
</head>
<body>
$END$ <%-- 内容占位符,表示编辑器光标默认位置 --%>
</body>
</html>
这是一个基础的 JSP 模板文件,主要包含:
文件信息注释
IDE 创建信息
创建者信息
创建时间
模板修改提示
页面指令
<%@ page … %> - JSP页面指令
contentType=“text/html” - 指定内容类型为HTML
charset=UTF-8 - 使用UTF-8字符编码
language=“java” - 使用Java作为脚本语言
占位符
T
i
t
l
e
Title
Title - 标题占位符
E
N
D
END
END - 内容占位符
这是一个最基础的页面模板,通常用作:
新建JSP页面的起点
快速创建页面的模板
统一项目中JSP页面的基本结构
- pom.xml
:Maven配置文件
<?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="...">
<!-- 父项目配置 -->
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>nuc.ss</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springmvc-03-annotation</artifactId>
<!-- 构建配置 -->
<build>
<resources>
<!-- 配置Java源码目录下的资源文件处理 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- 配置资源目录下的资源文件处理 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
7 springmvc-03-annotation.iml 的注释说明:
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<!-- Web模块配置 -->
<facet type="web" name="Web">
<configuration>
<!-- Web应用描述符配置 -->
<descriptors>
<!-- web.xml的位置配置 -->
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<!-- Web资源根目录配置 -->
<webroots>
<!-- 指定web目录为Web应用的根目录 -->
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
<!-- 源代码根目录配置 -->
<sourceRoots>
<!-- Java源代码目录 -->
<root url="file://$MODULE_DIR$/src/main/java" />
<!-- 资源文件目录 -->
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
</module>
这些配置文件共同构成了一个基础的Spring MVC项目结构。主要的处理流程是:
- 用户请求首先被web.xml
- 中配置的DispatcherServlet接收
- 根据springmvc-servlet.xml的配置,请求被路由到对应的Controller
- Controller处理请求并返回视图名称
- 视图解析器解析视图名称,找到对应的JSP文件
- JSP文件被渲染后返回给用户
这样的结构提供了一个清晰的MVC分层,便于后续维护和扩展。
4 服务器设置 tomcat
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/18f6ec45b4f14210addd1b6a8df7cfed.png