(要啥复制啥)

    1. 创建Maven工程
    2. 依赖:

      1. <dependencies>
      2. <!-- SpringMVC -->
      3. <dependency>
      4. <groupId>org.springframework</groupId>
      5. <artifactId>spring-webmvc</artifactId>
      6. <version>5.3.1</version>
      7. </dependency>
      8. <!-- 日志 -->
      9. <dependency>
      10. <groupId>ch.qos.logback</groupId>
      11. <artifactId>logback-classic</artifactId>
      12. <version>1.2.3</version>
      13. </dependency>
      14. <!-- ServletAPI -->
      15. <dependency>
      16. <groupId>javax.servlet</groupId>
      17. <artifactId>javax.servlet-api</artifactId>
      18. <version>3.1.0</version>
      19. <scope>provided</scope> <!-- provided -->
      20. </dependency>
      21. <!-- Spring5和Thymeleaf整合包 -->
      22. <dependency>
      23. <groupId>org.thymeleaf</groupId>
      24. <artifactId>thymeleaf-spring5</artifactId>
      25. <version>3.0.12.RELEASE</version>
      26. </dependency>
      27. <!--@ResponseBody处理json所需要的依赖-->
      28. <dependency>
      29. <groupId>com.fasterxml.jackson.core</groupId>
      30. <artifactId>jackson-databind</artifactId>
      31. <version>2.12.1</version>
      32. </dependency>
      33. <!--文件上传-->
      34. <dependency>
      35. <groupId>commons-fileupload</groupId>
      36. <artifactId>commons-fileupload</artifactId>
      37. <version>1.3.1</version>
      38. </dependency>
      39. </dependencies>
    3. war,创建 webapp 包,创建web.xml文件

    image.png

    1. web.xml ```xml <?xml version=”1.0” encoding=”UTF-8”?>

      1. <filter-name>CharacterEncodingFilter</filter-name>
      2. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      3. <init-param>
      4. <param-name>encoding</param-name>
      5. <param-value>UTF-8</param-value>
      6. </init-param>
      7. <init-param>
      8. <param-name>forceResponseEncoding</param-name>
      9. <param-value>true</param-value>
      10. </init-param>

      1. <filter-name>CharacterEncodingFilter</filter-name>
      2. <url-pattern>/*</url-pattern>

      1. <filter-name>HiddenHttpMethodFilter</filter-name>
      2. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

      1. <filter-name>HiddenHttpMethodFilter</filter-name>
      2. <url-pattern>/*</url-pattern>

      1. <servlet-name>DispatcherServlet</servlet-name>
      2. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      3. <init-param>
      4. <param-name>contextConfigLocation</param-name>
      5. <param-value>classpath:springMVC.xml</param-value>
      6. </init-param>
      7. <load-on-startup>1</load-on-startup>

      1. <servlet-name>DispatcherServlet</servlet-name>
      2. <url-pattern>/</url-pattern>

    1. 5.springMVC.xml
    2. ```xml
    3. <?xml version="1.0" encoding="UTF-8"?>
    4. <beans xmlns="http://www.springframework.org/schema/beans"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    6. xmlns:context="http://www.springframework.org/schema/context"
    7. 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">
    8. <!--注解扫描-->
    9. <context:component-scan base-package="com.wexiao.restful.controller"></context:component-scan>
    10. <!--配置 thymeleaf 的视图解析器-->
    11. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    12. <property name="order" value="1"/>
    13. <property name="characterEncoding" value="UTF-8"/>
    14. <property name="templateEngine">
    15. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    16. <property name="templateResolver">
    17. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    18. <!-- 视图前缀 -->
    19. <property name="prefix" value="/WEB-INF/templates/"/>
    20. <!-- 视图后缀 -->
    21. <property name="suffix" value=".html"/>
    22. <property name="templateMode" value="HTML5"/>
    23. <property name="characterEncoding" value="UTF-8" />
    24. </bean>
    25. </property>
    26. </bean>
    27. </property>
    28. </bean>
    29. <!--配置视图控制器:当控制器方法中,仅仅用来实现页面跳转-->
    30. <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    31. <!--
    32. 处理静态资源,例如html、js、css、jpg
    33. 若只设置该标签,则只能访问静态资源,其他请求则无法访问
    34. 此时必须设置<mvc:annotation-driven/>解决问题
    35. -->
    36. <mvc:default-servlet-handler/>
    37. <!-- 开启mvc注解驱动 -->
    38. <mvc:annotation-driven>
    39. <mvc:message-converters>
    40. <!-- 处理响应中文内容乱码 -->
    41. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    42. <property name="defaultCharset" value="UTF-8" />
    43. <property name="supportedMediaTypes">
    44. <list>
    45. <value>text/html</value>
    46. <value>application/json</value>
    47. </list>
    48. </property>
    49. </bean>
    50. </mvc:message-converters>
    51. </mvc:annotation-driven>
    52. <!--配置文件上传解析器,将上传的文件封装为 MultipartFile对象-->
    53. <!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-->
    54. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    55. <!--配置拦截器-->
    56. <!--<mvc:interceptors>
    57. <bean class="com.atguigu.interceptor.FirstInterceptor"></bean> //FirstInterceptor拦截器类名 (implements HandlerInterceptor)
    58. <ref bean="firstInterceptor"></ref> //需要在FirstInterceptor类上标识普通组件Component(value="firstInterceptor")
    59. </mvc:interceptors>-->
    60. <!-- 以上两种配置方式都是对DispatcherServlet所处理的所有的请求进行拦截 -->
    61. <mvc:interceptors>
    62. <mvc:interceptor>
    63. <!--<mvc:mapping path="/*"/> <!--/a /b 一层目录--> -->
    64. <mvc:mapping path="/**"/> <!--拦截所有-->
    65. <mvc:exclude-mapping path="/"/>
    66. <ref bean="firstInterceptor"></ref>
    67. </mvc:interceptor>
    68. </mvc:interceptors>
    69. <!--
    70. 以上配置方式可以通过ref或bean标签设置拦截器,通过mvc:mapping设置需要拦截的请求,通过mvc:exclude-mapping设置需要排除的请求,即不需要拦截的请求
    71. -->
    72. <!-- 配置异常处理 -->
    73. <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    74. <property name="exceptionMappings">
    75. <props>
    76. <!--
    77. properties的键表示处理器方法执行过程中出现的异常
    78. properties的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面
    79. error:视图名称
    80. -->
    81. <prop key="java.lang.ArithmeticException">error</prop>
    82. </props>
    83. </property>
    84. <!--
    85. exceptionAttribute属性设置一个属性名,将出现的异常信息在请求域中进行共享,并设置在共享请求域中的键
    86. -->
    87. <property name="exceptionAttribute" value="exception"></property>
    88. <!--在html页面中, <p th:text="${exception}"></p> -->
    89. <!--show:java.lang.ArithmeticException -->
    90. </bean>
    91. </beans>