13. 注解配置SpringMVC
阅读原文时间:2023年09月05日阅读:7

使用配置类和注解代替web.xml和SpringMVC配置文件的功能

在Servlet3.0环境中,容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,

如果找到的话就用它来配置Servlet容器。 Spring提供了这个接口的实现,名为

SpringServletContainerInitializer,这个类反过来又会查找实现WebApplicationInitializer的类并将配

置的任务交给它们来完成。Spring3.2引入了一个便利的WebApplicationInitializer基础实现,名为

AbstractAnnotationConfigDispatcherServletInitializer,当我们的类扩展了

AbstractAnnotationConfigDispatcherServletInitializer并将其部署到Servlet3.0容器的时候,容器会自动发现它,并用它来配置Servlet上下文。

public class WebInit extends
    AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 指定spring的配置类
     * @return
     */
    @Override
 &nbsp; &nbsp;protected Class<?>[] getRootConfigClasses() {
 &nbsp; &nbsp; &nbsp; &nbsp;return new Class[]{SpringConfig.class};
 &nbsp;  }
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 指定SpringMVC的配置类
 &nbsp; &nbsp; * @return
 &nbsp; &nbsp; */
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;protected Class<?>[] getServletConfigClasses() {
 &nbsp; &nbsp; &nbsp; &nbsp;return new Class[]{WebConfig.class};
 &nbsp;  }
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 指定DispatcherServlet的映射规则,即url-pattern
 &nbsp; &nbsp; * @return
 &nbsp; &nbsp; */
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;protected String[] getServletMappings() {
 &nbsp; &nbsp; &nbsp; &nbsp;return new String[]{"/"};
 &nbsp;  }
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 添加过滤器
 &nbsp; &nbsp; * @return
 &nbsp; &nbsp; */
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;protected Filter[] getServletFilters() {
 &nbsp; &nbsp; &nbsp; &nbsp;CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
 &nbsp; &nbsp; &nbsp; &nbsp;encodingFilter.setEncoding("UTF-8");
 &nbsp; &nbsp; &nbsp; &nbsp;encodingFilter.setForceRequestEncoding(true);
 &nbsp; &nbsp; &nbsp; &nbsp;HiddenHttpMethodFilter hiddenHttpMethodFilter = newHiddenHttpMethodFilter();
 &nbsp; &nbsp; &nbsp; &nbsp;return new Filter[]{encodingFilter, hiddenHttpMethodFilter};
 &nbsp;  }
}


@Configuration
public class SpringConfig {
 &nbsp; &nbsp;//ssm整合之后,spring的配置信息写在此类中
}


@Configuration
//扫描组件
@ComponentScan("com.atguigu.mvc.controller")
//开启MVC注解驱动
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
 &nbsp; &nbsp;//使用默认的servlet处理静态资源
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
 &nbsp; &nbsp; &nbsp; &nbsp;configurer.enable();
 &nbsp;  }
 &nbsp; &nbsp;//配置文件上传解析器
 &nbsp; &nbsp;@Bean
 &nbsp; &nbsp;public CommonsMultipartResolver multipartResolver(){
 &nbsp; &nbsp; &nbsp; &nbsp;return new CommonsMultipartResolver();
 &nbsp;  }
 &nbsp; &nbsp;//配置拦截器
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;public void addInterceptors(InterceptorRegistry registry) {
 &nbsp; &nbsp; &nbsp; &nbsp;FirstInterceptor firstInterceptor = new FirstInterceptor();
 &nbsp; &nbsp; &nbsp; &nbsp;registry.addInterceptor(firstInterceptor).addPathPatterns("/**");
 &nbsp;  }
 &nbsp; &nbsp;//配置视图控制
 &nbsp; &nbsp;/*@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }*/
 &nbsp; &nbsp;//配置异常映射
 &nbsp; &nbsp;/*@Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
        Properties prop = new Properties();
        prop.setProperty("java.lang.ArithmeticException", "error");
        //设置异常映射
        exceptionResolver.setExceptionMappings(prop);
        //设置共享异常信息的键
        exceptionResolver.setExceptionAttribute("ex");
        resolvers.add(exceptionResolver);
    }*/
 &nbsp; &nbsp;//配置生成模板解析器
 &nbsp; &nbsp;@Bean
 &nbsp; &nbsp;public ITemplateResolver templateResolver() {
 &nbsp; &nbsp; &nbsp; &nbsp;WebApplicationContext webApplicationContext =ContextLoader.getCurrentWebApplicationContext();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过WebApplicationContext 的方法获得
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ServletContextTemplateResolver templateResolver = new
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ServletContextTemplateResolver(webApplicationContext.getServletContext());
 &nbsp; &nbsp; &nbsp; &nbsp;templateResolver.setPrefix("/WEB-INF/templates/");
 &nbsp; &nbsp; &nbsp; &nbsp;templateResolver.setSuffix(".html");
 &nbsp; &nbsp; &nbsp; &nbsp;templateResolver.setCharacterEncoding("UTF-8");
 &nbsp; &nbsp; &nbsp; &nbsp;templateResolver.setTemplateMode(TemplateMode.HTML);
 &nbsp; &nbsp; &nbsp; &nbsp;return templateResolver;
 &nbsp;  }
 &nbsp; &nbsp;//生成模板引擎并为模板引擎注入模板解析器
 &nbsp; &nbsp;@Bean
 &nbsp; &nbsp;public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
 &nbsp; &nbsp; &nbsp; &nbsp;SpringTemplateEngine templateEngine = new SpringTemplateEngine();
 &nbsp; &nbsp; &nbsp; &nbsp;templateEngine.setTemplateResolver(templateResolver);
 &nbsp; &nbsp; &nbsp; &nbsp;return templateEngine;
 &nbsp;  }
 &nbsp; &nbsp;//生成视图解析器并未解析器注入模板引擎
 &nbsp; &nbsp;@Bean
 &nbsp; &nbsp;public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
 &nbsp; &nbsp; &nbsp; &nbsp;ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
 &nbsp; &nbsp; &nbsp; &nbsp;viewResolver.setCharacterEncoding("UTF-8");
 &nbsp; &nbsp; &nbsp; &nbsp;viewResolver.setTemplateEngine(templateEngine);
 &nbsp; &nbsp; &nbsp; &nbsp;return viewResolver;
 &nbsp;  }
}


@RequestMapping("/")
public String index(){
    return "index";
}