利用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是成功的!

时间: 2024-10-10 23:05:33

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

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

基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键项目添加几个package: com.easyweb.config (保存项目配置) com.easyweb.controller (保存springMvc controller) 3.在 com.easyweb.config 新建一个类 WebApplicationStartup ,这个类实现We

springmvc在web.xml中的配置详解

                          <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-

springMVC的web.xml配置注意

web.xml需要放过所有资源文件,这个就看自己的系统中有哪些静态文件.一般的都是.js..css..jpg..png.jpeg等等,但是我还用到一些字体文件资源,所以也要过滤,不然前台会找不到改资源. <!-- springMVC核心配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.Dispatc

SpringMVC在web.xml配置文件

<servlet> <servlet-name>broadTransfer</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> &

配置spring的web.xml

1 配置web.xml ????<context-param> ????????<param-name>contextConfigLocation</param-name> ????????<param-value>classpath*:/applicationContext*.xml</param-value> ????</context-param> ????<listener> ????????<listene

eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置&lt;Servlet&gt;

eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet>图片和代码如下: web.xml中源代码如下: <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

如何通过配置tomcat或是web.xml让ie直接下载文件

web.xml(tomcat\conf\web.xml)中配置了 <mime-mapping>   <extension>txt</extension>   <mime-type>application/txt</mime-type> </mime-mapping> 原文地址:https://www.cnblogs.com/xiaoleiel/p/11160757.html

配置struts时web.xml中&lt;url-pattern&gt;*.action&lt;/url-pattern&gt;

<filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping>    <filter-name>struts2</fil

关于SpringMvc中web.xml配置的Servlet

<servlet>   <!-- 配置DispatcherServlet -->   <servlet-name>springMvc</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <!-- 指定spring mvc配置文件位置 不指定使用默认情况 -->     &l