利用WebApplicationInitializer配置SpringMVC取代web.xml
对于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的更多相关文章
- SpringMVC基于代码的配置方式(零配置,无web.xml)直接继承WebMvcConfigurerAdapter
基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...
- SpringMVC基于代码的配置方式(零配置,无web.xml)
基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
- SpringMVC的web.xml配置注意
web.xml需要放过所有资源文件,这个就看自己的系统中有哪些静态文件.一般的都是.js..css..jpg..png.jpeg等等,但是我还用到一些字体文件资源,所以也要过滤,不然前台会找不到. & ...
- SpringMVC配置文件-web.xml的配置
SpringMVC配置文件(重点) @Web.xml @核心拦截器(必配) <!-- spring 核心转发器,拦截指定目录下的请求,分配到配置的拦截路径下处理 --> <servl ...
- Spring 及 SpringMVC的web.xml配置详解
出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...
- springmvc配置文件web.xml详解各方总结(转载)
Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...
- springmvc配置文件web.xml详解各方总结。
Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...
- 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=" ...
随机推荐
- Python -- Scrapy 框架简单介绍(Scrapy 安装及项目创建)
Python -- Scrapy 框架简单介绍 最近在学习python 爬虫,先后了解学习urllib.urllib2.requests等,后来发现爬虫也有很多框架,而推荐学习最多就是Scrapy框架 ...
- javascript异步编程方案汇总剖析
code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; backgr ...
- 监督学习--k近邻算法
2017-07-20 15:18:25 k近邻(k-Nearest Neighbour, 简称kNN)学习是一种常用的监督学习方法,其工作机制非常简单,对某个给定的测试样本,基于某种距离度量找出训练集 ...
- English trip -- VC(情景课) 6 A Time
Words eleven 十一 twelve 十二 thirteen 十三 fourteen fifteen sixteen seventeen eighteen nineteen twenty ...
- 开发环境运行正常,发布服务器后提示HTTP 错误 403.14 - Forbidden
一.发布服务器后报错 今天在项目发布中遇到一件奇怪的事,开发完成的项目,发布到服务器上时 1. 发布到A服务器,一切正常 2. 发布到B服务器,提示403服务器错误 在同事电脑上重新打包发布代码,并发 ...
- Eureka服务注册过程详解之IpAddress(详解eureka.instance.prefer-ip-address = true 与 eureka.instance.prefer-ip-address)
分析,eureka.instance.prefer-ip-address 本节解释为什么配置eureka.instance.prefer-ip-address = true时,注册到Eureka Se ...
- MyBatis:4
转载:http://www.cnblogs.com/xrq730/p/5289638.html 什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相 ...
- zk客户端的ClientCnxn类
ClientCnxn是客户端的类:该类管理zk客户端的socket io,维持一个可用服务器的列表. //ClientCnxn类 private final LinkedList<Packet& ...
- 我所理解的event loop
灵魂三问 JS为什么是单线程的 我们都知道,JS是单线程的语言,那为什么呢?我的理解是JS设计之初就是为了在浏览器端完成DOM操作和一些简单交互的,既然涉及到DOM操作如果是多线程就会带来复杂的同步问 ...
- 信号的发送kill,raise,alarm,setitimer,abort,sigqueue
1.kill函数 int kill(pid_t pid, int sig); 发送信号给指定的进程. (1) If pid is positive, then signal sig is sent t ...