差不多文章

1、创建Maven工程,pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.wexiao.mvc</groupId>
  7. <artifactId>spring-annotation</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <dependencies>
  11. <!-- SpringMVC -->
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-webmvc</artifactId>
  15. <version>5.3.1</version>
  16. </dependency>
  17. <!-- 日志 -->
  18. <dependency>
  19. <groupId>ch.qos.logback</groupId>
  20. <artifactId>logback-classic</artifactId>
  21. <version>1.2.3</version>
  22. </dependency>
  23. <!-- ServletAPI -->
  24. <dependency>
  25. <groupId>javax.servlet</groupId>
  26. <artifactId>javax.servlet-api</artifactId>
  27. <version>3.1.0</version>
  28. <scope>provided</scope> <!-- provided -->
  29. </dependency>
  30. <!-- Spring5和Thymeleaf整合包 -->
  31. <dependency>
  32. <groupId>org.thymeleaf</groupId>
  33. <artifactId>thymeleaf-spring5</artifactId>
  34. <version>3.0.12.RELEASE</version>
  35. </dependency>
  36. </dependencies>
  37. </project>

2、写一个WebInit类继承AbstractAnnotationConfigDispatcherServletInitializer,实现三个方法

  1. //web工程的初始化类,用来代替web.xml
  2. public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
  3. /**
  4. * 指定spring的配置类
  5. * @return
  6. */
  7. protected Class<?>[] getRootConfigClasses() {
  8. return new Class[0];
  9. }
  10. /**
  11. * 指定springMVC的配置类
  12. * @return
  13. */
  14. protected Class<?>[] getServletConfigClasses() {
  15. return new Class[0];
  16. }
  17. /**
  18. * 指定DispatcherServlet的映射规则,即url-pattern
  19. * @return
  20. */
  21. protected String[] getServletMappings() {
  22. return new String[0];
  23. }
  24. }

3、创建SpringConfig、WebConfig类(使用@Configuration),修改上面方法的返回值

  1. @Configuration
  2. public class SpringConfig {
  3. //ssm整合之后,spring的配置信息写在此类中
  4. }
  1. public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
  2. /**
  3. * 指定spring的配置类
  4. * @return
  5. */
  6. protected Class<?>[] getRootConfigClasses() {
  7. return new Class[]{SpringConfig.class};
  8. }
  9. /**
  10. * 指定springMVC的配置类
  11. * @return
  12. */
  13. protected Class<?>[] getServletConfigClasses() {
  14. return new Class[]{WebConfig.class};
  15. }
  16. /**
  17. * 指定DispatcherServlet的映射规则,即url-pattern
  18. * @return
  19. */
  20. protected String[] getServletMappings() {
  21. return new String[]{"/"};
  22. }
  23. }

4、注册过滤器:在WebInit类中 按ctrl+o

image.png

  1. /**
  2. * 注册过滤器
  3. * @return
  4. */
  5. @Override
  6. protected Filter[] getServletFilters() {
  7. CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
  8. characterEncodingFilter.setEncoding("UTF-8");
  9. characterEncodingFilter.setForceResponseEncoding(true);
  10. HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
  11. return new Filter[]{characterEncodingFilter,hiddenHttpMethodFilter};
  12. }

5、编写WebConfig(SpringMVC.xml)

在WebConfig类实现WebMvcConfigurer接口,按Ctrl+o实现需要的方法
image.png

  1. package com.wexiao.mvc.config;
  2. import com.wexiao.mvc.interceptor.TestInterceptor;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.context.ContextLoader;
  7. import org.springframework.web.context.WebApplicationContext;
  8. import org.springframework.web.multipart.MultipartResolver;
  9. import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  10. import org.springframework.web.servlet.HandlerExceptionResolver;
  11. import org.springframework.web.servlet.ViewResolver;
  12. import org.springframework.web.servlet.config.annotation.*;
  13. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  14. import org.thymeleaf.spring5.SpringTemplateEngine;
  15. import org.thymeleaf.spring5.view.ThymeleafViewResolver;
  16. import org.thymeleaf.templatemode.TemplateMode;
  17. import org.thymeleaf.templateresolver.ITemplateResolver;
  18. import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
  19. import java.util.List;
  20. import java.util.Properties;
  21. /**
  22. * Description: SpringMVC
  23. * Created by wexiao on 2022/3/25 19:56
  24. *
  25. * @version 1.0
  26. */
  27. /**
  28. * 代替SpringMvc的配置文件
  29. * 1、扫描注解,2、视图解析器,3、view-controller,4、default-servlet-handler
  30. * 5、mvc注解驱动,6、文件上传解析器,7、异常处理,8、拦截器
  31. */
  32. @Configuration //将当前类标识为一个配置类
  33. @ComponentScan("com.wexiao.mvc.controller") //扫描注解
  34. @EnableWebMvc //开启MVC注解驱动
  35. public class WebConfig implements WebMvcConfigurer {
  36. //default-servlet-handler
  37. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  38. configurer.enable();
  39. }
  40. //拦截器
  41. public void addInterceptors(InterceptorRegistry registry) {
  42. TestInterceptor testInterceptor = new TestInterceptor();
  43. registry.addInterceptor(testInterceptor).addPathPatterns("/**"); //添加拦截路径
  44. //.addInterceptor(testInterceptor).excludePathPatterns(); //添加排除路径
  45. }
  46. //view-controller
  47. public void addViewControllers(ViewControllerRegistry registry) {
  48. registry.addViewController("/hello").setViewName("hello");
  49. }
  50. //异常处理(也可以通过@Bean)
  51. public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
  52. SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
  53. Properties prop = new Properties();
  54. prop.setProperty("java.long.ArithmeticException","error");
  55. exceptionResolver.setExceptionMappings(prop);
  56. exceptionResolver.setExceptionAttribute("exception"); //设置在请求域中信息的键,error.html
  57. resolvers.add(exceptionResolver);
  58. }
  59. //文件上传解析器 (运行时报错0.0)
  60. @Bean
  61. public MultipartResolver multipartResolver(){
  62. CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
  63. return commonsMultipartResolver;
  64. }
  65. //Thymeleaf视图解析器
  66. /*<!--配置 thymeleaf 的视图解析器-->
  67. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
  68. <property name="order" value="1"/>
  69. <property name="characterEncoding" value="UTF-8"/>
  70. <property name="templateEngine">
  71. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
  72. <property name="templateResolver">
  73. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
  74. <!-- 视图前缀 -->
  75. <property name="prefix" value="/WEB-INF/templates/"/>
  76. <!-- 视图后缀 -->
  77. <property name="suffix" value=".html"/>
  78. <property name="templateMode" value="HTML5"/>
  79. <property name="characterEncoding" value="UTF-8" />
  80. </bean>
  81. </property>
  82. </bean>
  83. </property>
  84. </bean>
  85. 下面三个方法 从里到外创建 bean,然后调用
  86. */
  87. //配置生成模板解析器
  88. @Bean
  89. public ITemplateResolver templateResolver() {
  90. WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
  91. // ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过WebApplicationContext 的方法获得
  92. ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(
  93. webApplicationContext.getServletContext());
  94. templateResolver.setPrefix("/WEB-INF/templates/");
  95. templateResolver.setSuffix(".html");
  96. templateResolver.setCharacterEncoding("UTF-8");
  97. templateResolver.setTemplateMode(TemplateMode.HTML);
  98. return templateResolver;
  99. }
  100. //生成模板引擎并为模板引擎注入模板解析器
  101. @Bean
  102. public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
  103. SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  104. templateEngine.setTemplateResolver(templateResolver);
  105. return templateEngine;
  106. }
  107. //生成视图解析器并未解析器注入模板引擎
  108. @Bean
  109. public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
  110. ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
  111. viewResolver.setCharacterEncoding("UTF-8");
  112. viewResolver.setTemplateEngine(templateEngine);
  113. return viewResolver;
  114. }
  115. }