文章目录
SpringMvc框架搭建(xml方式)
一、创建普通maven项目
1.配置pom.xml,添加依赖项
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.21</version>
</dependency>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!--tomcat自带有这个 范围配置了provided 代表已被提供 项目打成war包不会包含带有provided的包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--spring5和thymeleaf整合包-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
2.配置webapp目录
-
在main下新建webapp目录
-
打开项目结构 (快捷键:
ctrl+shift+alt+S
)注意:此时webapp就会显示小蓝点,否则就是路径写错了
3.配置web.xml文件
在默认的web.xml文件中
<?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">
<!--配置SpringMVC的前端控制器(DispatcherServlet),对浏览器发送的请求进行统一处理-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置SpringMVC配置文件的位置和名称-->
<init-param>
<!--contextConfigLocation:上下文配置路径-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--将前端控制器DispatcherServlet初始化时间提前到服务器初始化阶段-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置前端控制器的处理路径(接收请求的范围)-->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--
设置springMVC的核心控制器所能处理的请求路径请求
/ 所匹配的请求可以是/login或.html或.js或.css方式的请求路径
/ 不能匹配.jsp的请求路径的请求
/* 是包括所有请求 也包含.jsp
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.配置springMVC.xml文件
在resources资源目录下 需要注意一些包的声明指定需要存在
<?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"
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">
<!--扫描组件-->
<context:component-scan base-package="com.hopu.controller"></context:component-scan>
<!--配置Thymeleaf视图解析器 实现页面跳转-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!--设置视图解析器的优先级(代表多个视图解析器的先后顺序)-->
<property name="order" value="1"/>
<!--解析视图的编码-->
<property name="characterEncoding" value="UTF-8"/>
<!--模板-->
<property name="templateEngine">
<!--下面都是内部bean嵌套赋值 例如:下面这一行的bean为templateEngine赋值-->
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--视图前缀 要解析的视图名称(index) 会自动加上前缀(/WEB-INF/templates/) - index - 后缀(.html) 跳转到指定页面-->
<property name="prefix" value="/WEB-INF/templates/"/>
<!--视图后缀-->
<property name="suffix" value=".html"/>
<!--模板模型,用的html5-->
<property name="templateMode" value="HTML5"/>
<!--模板编码:utf-8-->
<property name="characterEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
5.创建html静态页面
因为在前端控制器中增加了视图前缀(/WEB-INF/templates),所以我们在这个位置创建我们的试图文件,当controller返回视图名字的时候,会被视图解析器解析增加前后缀跳转到指定页面的
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
首页啊
</body>
</html>
6.创建controller控制器
在springMVC.xml配置文件中,扫描组件的地址写入了com.hopu.controller,所以在此文件夹下创建我们的controller控制器 并且增加@controller注解,就称为控制器了
package com.hopu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/")
public String index(){
return "index";
}
}
现在就配置完成了,可以使用服务器部署访问了,在controller中,设置了 “/” 直接跳转到index.html首页。
二、总结
1.pom.xml文件中有个坑
<groupId>org.example</groupId>
<artifactId>SpringMvc2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> -- 这里需要指定打包类型,否则会404
2.心得
浏览器发送请求,若请求地址符合前端控制器的
url-pattern
,该请求就会被前端控制器DispatcherServlet
处理,前端控制器会读取SpringMVC
的核心配置文件,通过扫描组件找到控制器,将请求地址和控制器中@RequestMapping
注解的value
属性值进行匹配,若匹配成功,该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称,该视图名称会被视图解析器解析,加上前缀和后缀组成视图的路径,通过Thymeleaf
对视图进行渲染,最终转发到视图所对应页面