WebApplicationInitializer in Spring MVC
来源:互联网 发布:linux编辑文件的命令 编辑:程序博客网 时间:2024/05/16 13:39
Spring 3.1 has introduced a new feature for configuring the deployment descriptor using the Java Config approach. We don’t need to write the web.xml file for initializing the web applications. Everything can be done through a Java class itself. The Java class has to implement the org.springframework.web.WebApplicationInitializer which will be loaded when web application starts. If you look at the example, a class implementing WebApplicationInitializer loads the spring context configuration file.
Lets look at the example.
1. Spring MVC Controller
SpringMVCController.java
package javabeat.net.spring.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class SpringMVCController { @RequestMapping(value = "/springmvctest", method = RequestMethod.GET) public String redirectTest(Model model) { model.addAttribute("msg", "Test Message"); return "hello"; }}
2. WebApplicationInitializer
SpringWebAppInitializer.java
package javabeat.net.spring.controller;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.XmlWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;public class SpringWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring-dispatcher-servlet.xml"); ServletRegistration.Dynamic dispatcher = container.addServlet( "spring-dispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }}
3. Views
hello.jsp
<html><body> <h1>JavaBeat Spring MVC - WebApplicationInitializer</h1> <h2>Value : ${msg}</h2></body></html>
4. Spring Configurations
spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="javabeat.net.spring.controller" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean></beans>
5. Run WebApplicationInitializer using JavaConfig
You can just run the above spring mvc example without any extra changes. The class SpringWebAppInitializer will be loaded by the server and initialize the spring context. Note that, you have to use Spring 3.1 and Tomcat 7.0 to make it work. This feature is introduced from Servlet 3.0 release.
Download Spring MVC WebApplicationInitializer Example Source Code
- WebApplicationInitializer in Spring MVC
- Spring WebApplicationInitializer and ApplicationContextInitializer confusion
- spring框架的WebApplicationInitializer接口
- WebApplicationInitializer
- WebApplicationInitializer
- No Spring WebApplicationInitializer types detected on classpath
- No Spring WebApplicationInitializer types detected on classpath
- WebApplicationInitializer (spring 3.x.x以上版本)
- How to use Spring’s WebApplicationInitializer
- Strategy Pattern in Spring MVC
- Exception Handling in Spring MVC
- Exception Handling in Spring MVC
- Cookie 跨域 in Spring MVC
- 2、DispatcherServlet in Spring MVC
- Exception Handling in Spring MVC
- "No Spring WebApplicationInitializer types detected on classpath" 解决方案
- SSH整合:No Spring WebApplicationInitializer types detected on classpath
- 关于No Spring WebApplicationInitializer types detected on classpath的提示
- 负载均衡
- 《Java程序设计》第16周周四:GUI编程及文件对话框的使用
- UIScrollView的简单使用
- android发送/接收json数据
- 剑指offer:二维数组中的查找(数组)
- WebApplicationInitializer in Spring MVC
- 新锐房地产销售管理系统(部分流程)技术解析(九) 销售管理_认购签约
- oracle数据快速删除
- 设计模式之Singleton模式(笔记)
- 解密DNSPOD应对DDoS攻击招式!
- iOS: 如何正确的绘制1像素的线
- 一个很好用的PPT转换成PDF的转换器
- 结构体大小计算
- 关于Java浮点数运算精度丢失问题