目录
在web.xml中配置用于将请求和响应进行编码,以免中文乱码配置详情
1.认识Spring MVC框架
Sping MVC概述
Spring MVC的特点
DispatcherServlet
Spring MVC框架像许多其他MVC框架一样, 以请求为驱动 , 围绕DispatcherServlet,分派请求并提供其他功能
Spring 使用DispatcherServlet 前控制器模式实现 MVC
DispatcherServlet继承自HttpServlet 基类,负责将请求路由到预定目的地的主要控制器
Model 只是应用的数据
视图则由各种模板引擎来表示
从Spring 2.5开始,使用Java 5或者以上版本的用户可采用基于注解的controller声明方式
Spring MVC的工作原理
解读
客户端发送请求,前端控制器DispatcherServlet接收请求
DispatcherServlet调用映射处理器HandlerMapping,将URL映射到对应处理器Controller
Controller调用业务逻辑,完成业务处理,并将处理结果ModelAndView对象返给DispatcherServlet
DispatcherServlet将ModelAndView传给视图解析器ViewReslover进行解析
ViewReslover将解析结果View,返回给DispatcherServlet
DispatcherServlet根据View和Model渲染视图响应给客户端
2. 搭建Spring MVC框架
1.创建web项目模块
2.引入相关依赖
<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.flowerfog</groupId>
<artifactId>SpringMvcDemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.flowerfog</groupId>
<artifactId>MvcXml</artifactId>
<packaging>jar</packaging>
<name>MvcXml</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<org.springframework.version>6.0.2</org.springframework.version>
<org.junit.jupiter.api.version>5.3.1</org.junit.jupiter.api.version>
<org.projectlombok.version>1.18.30</org.projectlombok.version>
</properties>
<dependencies>
<!--spring web依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--junit5测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${org.junit.jupiter.api.version}</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
</dependencies>
<build>
<finalName>MvcXml</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
POM.xml使用变量
POM.xml如何指定jdk
3.添加Spring MVC的配置文件
resources下创建配置文件spring-mvc.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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 这里添加其他配置 -->
</beans>
4.配置前置控制器
DispatcherServlet配置详情
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自动加载spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<!--当值≥0时,启动时就加载;当值<0或不指定时,则表示第一次请求时加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet接受所有URL请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5.配置编码过滤器
在web.xml中配置
用于将请求和响应进行编码,以免中文乱码
配置详情
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自动加载spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<!--当值≥0时,启动时就加载;当值<0或不指定时,则表示第一次请求时加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet接受所有URL请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器,解决中文乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6.编写jsp文件
在WEB-INF文件夹下,新建views文件夹,用来统一管理所有的jsp页面
在views文件夹下新建hello.jsp页面
7.编写控制器
规划控制器的包
新建控制器类HelloController
控制器的逻辑解读
@Controller注解标识该类为控制器
@RequestMapping("/hello")注解表示用户请求访问hello方法的映射路径url
控制器中的方法我们称为Action
8.设置注解自动扫描
修改spring-mvc.xml
通过扫描将controller配置到Spring容器
<?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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 将控制器扫描到容器中 -->
<context:component-scan base-package="org.flowerfog.controller"/>
<!-- 开启SpringMVC框架的注解驱动 -->
<mvc:annotation-driven/>
</beans>
9.配置web服务器
部署并设置上下文路径
10.运行验证
视图地址的处理
配置视图解析器
在spring-mvc.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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 将控制器扫描到容器中 -->
<context:component-scan base-package="org.flowerfog.controller"/>
<!-- 开启SpringMVC框架的注解驱动 -->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
视图解析器在控制器返回视图时自动加上前缀和后缀
基于视图解析器给视图地址的简化
package org.flowerfog.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/index")
public String index(){
return "hello";
}
}