一、搭建框架
第一步 引入依赖
<!-- 1.SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 2.日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 3.ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- 4.Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<!-- 5.springMVC 处理 json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<!-- 6文件上传所需 依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
第二步 配置web.xml
- SpringMVC编码过滤器,必须在最前
CharacterEncodingFilter
类- 请求编码
encoding
—>UTF-8- 响应编码
forceResponseEncoding
—> true
- HiddenHttpMethodFilter过滤器
- 作用:SpringMVC提供的类,帮助将POST请求转换为DELETE或者PUT请求
- SpringMVC的前端控制器
<url-pattern>/</url-pattern>
这种方式不包括.jsp文件
<?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.配置SpringMVC的编码过滤器,必须在最前-->
<filter>
<filter-name>CharacterEncodingFilter</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>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2.配置HiddenHttpMethodFilter过滤器-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 3.配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>demo4</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo4</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第三步 创建核心配置文件springmvc.xml
在resources目录下创建springmvc.xml文件
所有配置:
- 开启包扫描 context:component-scan
- 配置Thymeleaf解析器
- 开启注解驱动 mvc:annotation
- 配置无操作页面跳转 mvc:view-controller
- 开启对静态资源的访问 mvc:default-servlet-handler
- 配置文件上传解析器 bean class=CommonsMultipartResolver
- 指定拦截器配置 mvc:interceptors
- 配置自定义异常处理器 bean SimpleMappingExceptionResolver
按需配置,删除不需要的配置语句
<?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">
<!-- 1.开启包扫描-->
<context:component-scan base-package="com.cqy"/>
<!-- 2.配置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 class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<!-- 3.配置一些没有操作的页面跳转,例如首页 -->
<mvc:view-controller path="/" view-name="index"/>
<!-- 4.开启对静态资源的访问-->
<mvc:default-servlet-handler/>
<!-- 5.开启注解驱动-->
<mvc:annotation-driven/>
<!-- 必须通过文件解析器的解析才能将文件转换为MultipartFile对象 且必须添加id属性值必须为 multipartResolver-->
<!-- 6.配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<!-- 7.针对指定拦截器的配置 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 指定拦截路径
/* 代表拦截一级的全部路径,当控制器中有 /**/xxx时,多层目录无法拦截
/** 代表拦截全部路径
-->
<mvc:mapping path="/**"/>
<!-- 指定排除的拦截路径 -->
<mvc:exclude-mapping path="/"/>
<!-- 指定拦截器一下方式二选一 -->
<!-- <bean class="com.cqy.interceptor.FirstInterceptor"></bean>-->
<ref bean="firstInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 8.配置自定义异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!--
properties:的键表示处理器方法执行过程中出现的异常
properties:的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面,可以是forward或者redirect
-->
<prop key="java.lang.ArithmeticException">error</prop>
</props>
</property>
<!--
exceptionAttribute属性设置一个属性名,将出现的异常信息在请求域中进行共享,可以在页面中获取
-->
<property name="exceptionAttribute" value="ex"/>
</bean>
二、添加测试代码
创建controller方法
@Controller
public class HelloController {
//由于在springmvc.xml中配置了index的页面跳转,所以在这里就不使用@RequestMapping
//@RequestMapping("/")
public String index() {
return "index";
}
}
创建index.html页面
在WEB-INF文件夹下创建templates目录,在该目录下创建index.html
注意:要在html标签中加入 xmlns:th="http://www.thymeleaf.org"
,引入Thymeleaf模版引擎
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/target}">访问目标页面</a>
</body>
</html>
至此SpringMVC框架搭建完毕