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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<code class="hljs" java="">package com.easyweb.config;
 
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
 
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
 
/**
 * 服务器启动入口类
 *
 * @author Administrator
 *
 */
public class WebApplicationStartup implements WebApplicationInitializer {
 
  private static final String SERVLET_NAME = Spring-mvc;
 
  private static final long MAX_FILE_UPLOAD_SIZE = 1024 * 1024 * 5; // 5 Mb
 
  private static final int FILE_SIZE_THRESHOLD = 1024 * 1024; // After 1Mb
 
  private static final long MAX_REQUEST_SIZE = -1L; // No request size limit
 
  /**
   * 服务器启动调用此方法,在这里可以做配置 作用与web.xml相同
   */
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    // 注册springMvc的servlet
    this.addServlet(servletContext);
    // 注册过滤器
    // servletContext.addFilter(arg0, arg1)
    // 注册监听器
    // servletContext.addListener(arg0);
  }
 
  /**
   * 注册Spring servlet
   *
   * @param servletContext
   */
  private void addServlet(ServletContext servletContext) {
    // 构建一个application context
    AnnotationConfigWebApplicationContext webContext = createWebContext(SpringMVC.class, ViewConfiguration.class);
    // 注册spring mvc 的 servlet
    Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(webContext));
    // 添加springMVC 允许访问的Controller后缀
    dynamic.addMapping(*.html, *.ajax, *.css, *.js, *.gif, *.jpg, *.png);
    // 全部通过请用 “/”
    // dynamic.addMapping(/);
    dynamic.setLoadOnStartup(1);
    dynamic.setMultipartConfig(new MultipartConfigElement(null, MAX_FILE_UPLOAD_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
  }
 
  /**
   * 通过自定义的配置类来实例化一个Web Application Context
   *
   * @param annotatedClasses
   * @return
   */
  private AnnotationConfigWebApplicationContext createWebContext(Class<!--?-->... annotatedClasses) {
    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.register(annotatedClasses);
 
    return webContext;
  }
 
}</code>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<code class="hljs" java="">package com.easyweb.config;
 
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
@Configuration
@EnableWebMvc
//指明controller所在的包名
@ComponentScan(basePackages = {com.easyweb.controller})
public class SpringMVC extends WebMvcConfigurerAdapter {
 
  /**
   * 非必须
   */
  @Override
  public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }
 
  /**
   * 如果项目的一些资源文件放在/WEB-INF/resources/下面
   * 在浏览器访问的地址就是类似:https://host:port/projectName/WEB-INF/resources/xxx.css
   * 但是加了如下定义之后就可以这样访问:
   * 非必须
   */
  @Override
  public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler(/resources/**/*).addResourceLocations(/WEB-INF/resources/);
  }
}</code>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<code avrasm="" class="hljs">package com.easyweb.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
 
@Configuration
public class ViewConfiguration {
 
    @Bean
    public ViewResolver urlBasedViewResolver() {
        UrlBasedViewResolver viewResolver;
        viewResolver = new UrlBasedViewResolver();
        viewResolver.setOrder(2);
        viewResolver.setPrefix(/WEB-INF/);
        viewResolver.setSuffix(.jsp);
        viewResolver.setViewClass(JstlView.class);
        // for debug envirment
        viewResolver.setCache(false);
        return viewResolver;
    }
    @Bean
    public ViewResolver tilesViewResolver() {
        UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
        urlBasedViewResolver.setOrder(1);
        urlBasedViewResolver.setViewClass(TilesView.class);
        //urlBasedViewResolver.
        return urlBasedViewResolver;
    }
    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { classpath:tiles.xml });
        return tilesConfigurer;
    }
}</code>

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

SpringMVC基于代码的配置方式(零配置,无web.xml)直接继承WebMvcConfigurerAdapter的更多相关文章

  1. WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

    本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...

  2. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

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

    基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...

  4. springmvc学习指南 之---第27篇 spring如何实现servlet3.0无web.xml 配置servlet对象的

    writedby 张艳涛 基于web.xml配置,有人说麻烦,tomcat给按照servlet3.0,实现了基于注解@WebServlet,有人说springmvc的springmvc.xml配置麻烦 ...

  5. 框架源码系列四:手写Spring-配置(为什么要提供配置的方法、选择什么样的配置方式、配置方式的工作过程是怎样的、分步骤一个一个的去分析和设计)

    一.为什么要提供配置的方法 经过前面的手写Spring IOC.手写Spring DI.手写Spring AOP,我们知道要创建一个bean对象,需要用户先定义好bean,然后注册到bean工厂才能创 ...

  6. Centos6/7系统基础配置-从零到无

    转至:https://www.cnblogs.com/Pigs-Will-Fly/p/13855300.html 目录 前言 系统配置 文档作用 一.Centos 6.X 系列配置 1.1  主机名 ...

  7. SpringMVC 学习 八 SSM环境搭建(一) web.xml配置

    第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...

  8. SpringMVC 零配置 无web.xml

    对SpringMVC启动流程的讲解 https://www.cnblogs.com/beiyan/p/5942741.html 与SpringMVC的整合 https://hanqunfeng.ite ...

  9. 配置 struts2 时掉进 web.xml 的坑

    这个声明不好用 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quo ...

随机推荐

  1. 关于Hibernate中的临时态, 持久态, 游离态

    三态的基本概念: 1, 临时状态(Transient):也叫自由态,只存在于内存中,而在数据库中没有相应数据.用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫临时对象: ...

  2. Java switch case和数组

    Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句格式: switch(express ...

  3. Arduino可穿戴教程之第一个程序——Blink(一)

    Arduino可穿戴教程之第一个程序——Blink(一) 至此我们的硬件和软件部分都准备好了,是时候测试一下他们是否可以和谐地合作了.当然,第一个程序我们并不需要自己来写,因为我们还没有了解过Ardu ...

  4. openstack vm实例pxe无法启动

    问题如下: 创建vm没有任何报错,打开控制台提示: SeaBIOS (versio xxxxxxx) Machine UUID xxxxxxxxxx iPXE (http://ipxe.org) 00 ...

  5. [BJOI2011]禁忌 --- AC自动机 + 矩阵优化 + 期望

    bzoj 2553 [BJOI2011]禁忌 题目描述: Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi ...

  6. BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp

    http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...

  7. django深入-ORM操作

    1 ORM添加 1.1 一对多添加 方式一: pub_obj=Publish.objects.get(id=2) Book.objects.create(title="python" ...

  8. 今天测试了一下 sqlalchemy 性能

    self.db.query(Users).filter(Users.Id==1).first() < self.db.execute('SELECT *  FROM `users` WHERE ...

  9. Open Source Universal 48 pin programmer design

    http://www.edaboard.com/thread227388.html Hi, i have designed a 48 pin universal programmer but need ...

  10. js:对象的创建(为prototype做铺垫)

    /**  *在js中并不存在类,所以能够直接通过Object来创建对象,可是使用这样的方式创建有一  *弊端:因为没有类的约束,无法实现对象的反复利用,而且没有一种规范约定,在操作时easy带来问题. ...