在工程的类路径即 src 目录下创建 SpringMVC 的配置文件 springmvc.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"><!--配置扫描组件,创建对象、自动装配--><context:component-scan base-package="com.wzy.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 class="org.thymeleaf.spring5.SpringTemplateEngine"><!--内部bean--><property name="templateResolver"><bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><!--内部bean--><!-- 视图前缀 --><property name="prefix" value="/WEB-INF/templates/"/><!-- 视图后缀 --><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><!--使用的页面模板为 HTML5--><property name="characterEncoding" value="UTF-8" /><!--页面的编码--></bean></property></bean></property></bean><!--处理静态资源,例如html、js、css、jpg若只设置该标签,则只能访问静态资源,其他请求则无法访问此时必须设置<mvc:annotation-driven/>解决问题--><mvc:default-servlet-handler/><!-- 开启mvc注解驱动 --><mvc:annotation-driven><mvc:message-converters><!-- 处理响应中文内容乱码 --><bean class="org.springframework.http.converter.StringHttpMessageConverter"><property name="defaultCharset" value="UTF-8" /><property name="supportedMediaTypes"><list><value>text/html</value><value>application/json</value></list></property></bean></mvc:message-converters></mvc:annotation-driven></beans>
