对于Spring MVC的DispatcherServlet配置方式,传统的是基于XML方式的,也就是官方说明的XML-based,如下:

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

但是Spring文档建议我们采用code-based这种方式,当然,核心就是实现WebApplicationInitializer这个接口,查看这个接口的源码,里面也非常简单,只有一个方法,传入的参数是ServletContext:

void onStartup(ServletContext servletContext) throws ServletException;

下面是一个小例子:

1.项目结构:

2.MyWebAppInitializer.java

public class MyWebAppInitializer implements WebApplicationInitializer{

    public void onStartup(ServletContext servletContext) throws ServletException {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:spring-mvc.xml");
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
} }

3.UserController.java

@Controller
public class UserController { @RequestMapping("/show")
public String show(){
return "show";
}
}

4.spring-mvc.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.aijava.springcode"/> <mvc:annotation-driven /> <!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>

5.show.jsp

简单的一句打印:

<body>
show page;
</body>

6.启动Tomcat后输入url后发现是会出现结果的,code-based是成功的!

利用WebApplicationInitializer配置SpringMVC取代web.xml的更多相关文章

  1. SpringMVC基于代码的配置方式(零配置,无web.xml)直接继承WebMvcConfigurerAdapter

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

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

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

  3. spring 和springmvc 在 web.xml中的配置

    (1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...

  4. SpringMVC的web.xml配置注意

    web.xml需要放过所有资源文件,这个就看自己的系统中有哪些静态文件.一般的都是.js..css..jpg..png.jpeg等等,但是我还用到一些字体文件资源,所以也要过滤,不然前台会找不到. & ...

  5. SpringMVC配置文件-web.xml的配置

    SpringMVC配置文件(重点) @Web.xml @核心拦截器(必配) <!-- spring 核心转发器,拦截指定目录下的请求,分配到配置的拦截路径下处理 --> <servl ...

  6. Spring 及 SpringMVC的web.xml配置详解

    出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...

  7. springmvc配置文件web.xml详解各方总结(转载)

    Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...

  8. springmvc配置文件web.xml详解各方总结。

    Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...

  9. springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml

    springMVC注解:@Controller @Service @Repository 分别标注于web层,service层,dao层. web.xml <?xml version=" ...

随机推荐

  1. python的print

    grid=[['.', '.', '.', '.', '.', '.'], [', '.', '.', '.'], [', '.', '.'], [', '.'], ['], [', '.'], [' ...

  2. Unity + NGUI 实现人物头顶UI的信息展示

    1.思路: (1)信息数据:需要展示属性信息 (2)信息的展示:负责显示UI属性信息 (3)UI的跟随:负责实现UI对人物的跟随 (4)UI的管理:负责对UI进行创建于回收,游戏中需要用到UI的地方都 ...

  3. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  4. 使用mothur进行OTU聚类

    微生物16S的OTU聚类工具有很多,最常用的就是 usearch.cdhit-OTU.mothur. 这些工具大多都是针对二代测序平台的,usearch的64bit版本是收费的. 如果要跑PacBio ...

  5. 20170814xlVBA PowerPoint分类插图加说明

    Public Sub AddPictures() Dim ppApp As PowerPoint.Application Set ppApp = New PowerPoint.Application ...

  6. Confluence 6 从外部目录中同步数据如何工作

    下面是有关缓存功能的一些摘要信息: 用户和用户组的缓存信息保存在应用程序的数据库中. 当你连接一个新的外部目录到系统中的时候,一个同步任务将会启动被,并且在后台运行拷贝所有需要的用户和用户组信息,以及 ...

  7. ubuntu下使用CAJ云阅读--CAJViewer(Cloud)

    摘要:Linux(Ubuntu)没有直接打开caj论文格式的软件.网上流传最多的“CAJViewer6.0_green”.“CAJViewer7.2”都没法正常使用,所以迫切需要新的方法或软件;我发现 ...

  8. 『cs231n』循环神经网络RNN

    循环神经网络 循环神经网络介绍摘抄自莫凡博士的教程 序列数据 我们想象现在有一组序列数据 data 0,1,2,3. 在当预测 result0 的时候,我们基于的是 data0, 同样在预测其他数据的 ...

  9. thinkphp关于时间加减几天

    1.当前时间,往后退5天: date('Y-m-d H:i:s',strtotime('-1 days')); 2.有固定时间,往后面退一天或者七天,或者30天: 比如时间:$time = 2014- ...

  10. hdu-1907-反nim博弈

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...