码迷,mamicode.com
首页 > 编程语言 > 详细

springMvc项目的搭建,暂时没有整合持久层框架(java Config配置对比xml配置)

时间:2017-12-16 18:44:09      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:mon   text   int   otc   att   pos   this   isp   javac   

public class WebInit implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        //项目启动则执行 : 
        //前端控制器
        //对比xml配置,配置在web.xml中的
        /*
         * <servlet>
         *      <servlet-name>dispatcherServlet</servlet-name>
         *      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         *      <init-param>
         *          <param-name>contextConfigLocation</param-name>
         *          <param-value>/WEB-INF/classes/application.xml</param-value>
         *      </init-param>
         *      <load-on-startup>1</load-on-startup>
         * </servlet>
         * <servlet-mapping>
         *      <servlet-name>dispatherServlet</servlet-name>
         *      <url-pattern>*.do</url-pattern>
         * </servlet-mapping>
         * */
        //javaConfig配置
        //这句相当于控制器核心类
        ServletRegistration.Dynamic dispatcherServletRegistration = container.addServlet("dispatcher", new DispatcherServlet());
        //这句相当于配置服务器启动就加载servlet容器
        dispatcherServletRegistration.setLoadOnStartup(1);
        //这句相当于配置注解驱动
        dispatcherServletRegistration.setInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
        //这句相当于加载springmvc核心配置文件
        dispatcherServletRegistration.setInitParameter("contextConfigLocation", "com.mike.small.config.SpringMvcConfig");
        //拦截所有url
        dispatcherServletRegistration.addMapping("/");

        //处理乱码过滤器
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        FilterRegistration.Dynamic characterEncodingFilterRegistration = container.addFilter("characterEncodingFilter", filter);
        characterEncodingFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");

        //注解驱动类
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(SpringMvcConfig.class);
        //这句相当于web.xml的监听器
        /*<listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
         */
        container.addListener(new ContextLoaderListener(rootContext));
    }

}

  

@Configuration // this class contains bean definitions
@EnableWebMvc // same as <mvc:annotation-driven />
@ComponentScan(basePackages = {"com.mike.small"})
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // declare static resources
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
    
    @Bean
    public InternalResourceViewResolver viewResolver() {
        // view resolver
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
    
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(104857600);
        return multipartResolver;
    }
}

  

springMvc项目的搭建,暂时没有整合持久层框架(java Config配置对比xml配置)

标签:mon   text   int   otc   att   pos   this   isp   javac   

原文地址:http://www.cnblogs.com/liyong888/p/8047040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!