zoukankan      html  css  js  c++  java
  • Spring MVC零配置(全注解)(版本5.0.7)

    // 核心配置类
    package spittr.config;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            // TODO Auto-generated method stub
            return new Class<?>[] {RootConfig.class};
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            // 指定配置类
            return new Class<?>[] {WebConfig.class};
        }
        /**
         * 将一个或多个路径映射到DispatcherServlet上
         */
        @Override
        protected String[] getServletMappings() {
            // 将DispatcherServlet映射到“/”
            return new String[] {"/"};
        }
    
    }
    
    
    package spittr.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @Configuration
    @EnableWebMvc // 启用Spring MVC
    @ComponentScan("spittr.web") // 启用组件扫描
    public class WebConfig implements WebMvcConfigurer {
        /**
         * 配置JSP视图解析器
         * 
         * @return
         */
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            resolver.setExposeContextBeansAsAttributes(true);
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
    }
    
    
    package spittr.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    @Configuration
    @ComponentScan(basePackages= {"spitter"},
            excludeFilters= {
                    @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
                    })
    public class RootConfig {
    
    }
    
    
    
    package spittr.web; 
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HomeController {
        static {
            System.out.println("=============HomeController============");
        }
        @RequestMapping("/home")
        public String home() {
            System.out.println("hellow");
            return "home";
        }
    }
    
    
  • 相关阅读:
    阿里Canal中间件的初步搭建和使用
    深入理解Java String类
    深入理解Java中的String
    Docker 容器数据卷(挂载)
    在docker容器下利用数据卷实现在删除了mysql容器或者镜像的情况下恢复数据
    EFK(Elasticsearch+Filebeat+Kibana)收集容器日志
    Docker 容器日志占用空间过大解决办法
    四种软件架构,看看你属于哪个层次
    Kubernetes 如何只授予某一 Namespace 的访问权限
    Docker实验Docker的网络配置
  • 原文地址:https://www.cnblogs.com/caoleiCoding/p/9270510.html
Copyright © 2011-2022 走看看