在工程的类路径即 src 目录下创建 SpringMVC 的配置文件 springmvc.xml。该文件名可以任意命名。
    image.png

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. 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">
    7. <!--配置扫描组件,创建对象、自动装配-->
    8. <context:component-scan base-package="com.wzy.controller"></context:component-scan>
    9. <!-- 配置Thymeleaf视图解析器,解析符合条件的请求,然后进行页面跳转 -->
    10. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    11. <property name="order" value="1"/><!--视图解析器可配置多个,可用此标签设置优先级-->
    12. <property name="characterEncoding" value="UTF-8"/><!--设置字符集-->
    13. <property name="templateEngine"><!--当前模板-->
    14. <bean class="org.thymeleaf.spring5.SpringTemplateEngine"><!--内部bean-->
    15. <property name="templateResolver">
    16. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><!--内部bean-->
    17. <!-- 视图前缀 -->
    18. <property name="prefix" value="/WEB-INF/templates/"/>
    19. <!-- 视图后缀 -->
    20. <property name="suffix" value=".html"/>
    21. <property name="templateMode" value="HTML5"/><!--使用的页面模板为 HTML5-->
    22. <property name="characterEncoding" value="UTF-8" /><!--页面的编码-->
    23. </bean>
    24. </property>
    25. </bean>
    26. </property>
    27. </bean>
    28. <!--
    29. 处理静态资源,例如html、js、css、jpg
    30. 若只设置该标签,则只能访问静态资源,其他请求则无法访问
    31. 此时必须设置<mvc:annotation-driven/>解决问题
    32. -->
    33. <mvc:default-servlet-handler/>
    34. <!-- 开启mvc注解驱动 -->
    35. <mvc:annotation-driven>
    36. <mvc:message-converters>
    37. <!-- 处理响应中文内容乱码 -->
    38. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    39. <property name="defaultCharset" value="UTF-8" />
    40. <property name="supportedMediaTypes">
    41. <list>
    42. <value>text/html</value>
    43. <value>application/json</value>
    44. </list>
    45. </property>
    46. </bean>
    47. </mvc:message-converters>
    48. </mvc:annotation-driven>
    49. </beans>