SpringMVC基于代码的配置方式(零配置,无web.xml)

基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中。

1.创建一个动态web项目(无需web.xml)

2.右键项目添加几个package: com.easyweb.config (保存项目配置) com.easyweb.controller (保存springMvc controller)

3.在 com.easyweb.config 新建一个类 WebApplicationStartup ,这个类实现WebApplicationInitializer 接口,是项目的入口,作用类似于web.xml,具体代码如下:

packagecom.easyweb.config;
 
importjavax.servlet.MultipartConfigElement;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRegistration.Dynamic;
 
importorg.springframework.web.WebApplicationInitializer;
importorg.springframework.web.context.support.AnnotationConfigWebApplicationContext;
importorg.springframework.web.servlet.DispatcherServlet;
 
/**
 * 服务器启动入口类
 *
 * @author Administrator
 *
 */
publicclass WebApplicationStartup implementsWebApplicationInitializer {
 
  privatestatic final String SERVLET_NAME = Spring-mvc;
 
  privatestatic final long MAX_FILE_UPLOAD_SIZE = 1024* 1024* 5;// 5 Mb
 
  privatestatic final int FILE_SIZE_THRESHOLD = 1024* 1024;// After 1Mb
 
  privatestatic final long MAX_REQUEST_SIZE = -1L; // No request size limit
 
  /**
   * 服务器启动调用此方法,在这里可以做配置 作用与web.xml相同
   */
  @Override
  publicvoid onStartup(ServletContext servletContext) throwsServletException {
    // 注册springMvc的servlet
    this.addServlet(servletContext);
    // 注册过滤器
    // servletContext.addFilter(arg0, arg1)
    // 注册监听器
    // servletContext.addListener(arg0);
  }
 
  /**
   * 注册Spring servlet
   *
   * @param servletContext
   */
  privatevoid addServlet(ServletContext servletContext) {
    // 构建一个application context
    AnnotationConfigWebApplicationContext webContext = createWebContext(SpringMVC.class, ViewConfiguration.class);
    // 注册spring mvc 的 servlet
    Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, newDispatcherServlet(webContext));
    // 添加springMVC 允许访问的Controller后缀
    dynamic.addMapping(*.html, *.ajax, *.css, *.js, *.gif, *.jpg, *.png);
    // 全部通过请用 “/”
    // dynamic.addMapping(/);
    dynamic.setLoadOnStartup(1);
    dynamic.setMultipartConfig(newMultipartConfigElement(null, MAX_FILE_UPLOAD_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
  }
 
  /**
   * 通过自定义的配置类来实例化一个Web Application Context
   *
   * @param annotatedClasses
   * @return
   */
  privateAnnotationConfigWebApplicationContext createWebContext(Class ... annotatedClasses) {
    AnnotationConfigWebApplicationContext webContext = newAnnotationConfigWebApplicationContext();
    webContext.register(annotatedClasses);
 
    returnwebContext;
  }
 
}

4.在com.easyweb.config 下添加类 SpringMVC 继承 WebMvcConfigurerAdapter,这个类的作用是进行SpringMVC的一些配置,代码如下:


packagecom.easyweb.config;
 
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
importorg.springframework.web.servlet.config.annotation.EnableWebMvc;
importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
@Configuration
@EnableWebMvc
//指明controller所在的包名
@ComponentScan(basePackages = {com.easyweb.controller})
publicclass SpringMVC extendsWebMvcConfigurerAdapter {
 
  /**
   * 非必须
   */
  @Override
  publicvoid configureDefaultServletHandling(finalDefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }
 
  /**
   * 如果项目的一些资源文件放在/WEB-INF/resources/下面
   * 在浏览器访问的地址就是类似:http://host:port/projectName/WEB-INF/resources/xxx.css
   * 但是加了如下定义之后就可以这样访问:
   *http://host:port/projectName/resources/xxx.css
   * 非必须
   */
  @Override
  publicvoid addResourceHandlers(finalResourceHandlerRegistry registry) {
    registry.addResourceHandler(/resources/**/*).addResourceLocations(/WEB-INF/resources/);
  }
}


5.添加view配置文件com.easyweb.config下新建类ViewConfiguration,里面可以根据自己的需要定义视图拦截器:

packagecom.easyweb.config;
 
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.ViewResolver;
importorg.springframework.web.servlet.view.JstlView;
importorg.springframework.web.servlet.view.UrlBasedViewResolver;
importorg.springframework.web.servlet.view.tiles2.TilesConfigurer;
importorg.springframework.web.servlet.view.tiles2.TilesView;
 
@Configuration
publicclass ViewConfiguration {
 
    @Bean
    publicViewResolver urlBasedViewResolver() {
        UrlBasedViewResolver viewResolver;
        viewResolver = newUrlBasedViewResolver();
        viewResolver.setOrder(2);
        viewResolver.setPrefix(/WEB-INF/);
        viewResolver.setSuffix(.jsp);
        viewResolver.setViewClass(JstlView.class);
        // for debug envirment
        viewResolver.setCache(false);
        returnviewResolver;
    }
    @Bean
    publicViewResolver tilesViewResolver() {
        UrlBasedViewResolver urlBasedViewResolver = newUrlBasedViewResolver();
        urlBasedViewResolver.setOrder(1);
        urlBasedViewResolver.setViewClass(TilesView.class);
        //urlBasedViewResolver.
        returnurlBasedViewResolver;
    }
    @Bean
    publicTilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = newTilesConfigurer();
        tilesConfigurer.setDefinitions(newString[] { classpath:tiles.xml });
        returntilesConfigurer;
    }
}

6.本例中还用了tiles视图解析器,替换了原始的include方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值