-
-
Save superalsrk/76afec03302cd168cba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.datafox.web.config; | |
import org.sitemesh.config.ConfigurableSiteMeshFilter; | |
import org.springframework.web.WebApplicationInitializer; | |
import org.springframework.web.context.ContextLoaderListener; | |
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | |
import org.springframework.web.filter.CharacterEncodingFilter; | |
import org.springframework.web.servlet.DispatcherServlet; | |
import javax.servlet.FilterRegistration; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRegistration; | |
/** | |
* Created on 2015/6/29. | |
* @author Yasenia | |
*/ | |
public class WebAppInitializer implements WebApplicationInitializer { | |
@Override | |
public void onStartup(ServletContext container) throws ServletException { | |
// 配置 Spring 上下文 | |
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); | |
rootContext.register(ContextConfig.class, SecurityConfig.class); | |
// 配置 Spring 加载监听器 | |
container.addListener(new ContextLoaderListener(rootContext)); | |
// 配置 Spring mvc Servlet | |
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); | |
dispatcherContext.register(MvcConfig.class); | |
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); | |
dispatcher.setLoadOnStartup(1); | |
dispatcher.addMapping("/"); | |
// 配置字符编码过滤 | |
FilterRegistration.Dynamic characterEncodingFilter = container.addFilter("characterEncodingFilter", CharacterEncodingFilter.class); | |
characterEncodingFilter.setInitParameter("encoding", "UTF-8"); | |
characterEncodingFilter.setInitParameter("forceEncoding", "true"); | |
characterEncodingFilter.addMappingForUrlPatterns(null, true, "/*"); | |
// 配置 Sitemesh 过滤 | |
FilterRegistration.Dynamic sitemeshFilter = container.addFilter("sitemeshFilter", ConfigurableSiteMeshFilter.class); | |
sitemeshFilter.addMappingForUrlPatterns(null, true, "/*"); | |
} | |
} | |
/* | |
等价于以下web.xml配置 | |
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<!-- 配置 Spring 上下文 --> | |
<context-param> | |
<param-name>contextClass</param-name> | |
<param-value> | |
org.springframework.web.context.support.AnnotationConfigWebApplicationContext | |
</param-value> | |
</context-param> | |
<context-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value> | |
cn.datafox.web.config.ContextConfig | |
</param-value> | |
</context-param> | |
<!-- 配置 Spring 加载监听器 --> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
</listener> | |
<!-- 配置字符编码过滤 --> | |
<filter> | |
<filter-name>characterEncodingFilter</filter-name> | |
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> | |
<init-param> | |
<param-name>encoding</param-name> | |
<param-value>UTF-8</param-value> | |
</init-param> | |
<init-param> | |
<param-name>forceEncoding</param-name> | |
<param-value>true</param-value> | |
</init-param> | |
</filter> | |
<filter-mapping> | |
<filter-name>characterEncodingFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
<!-- 配置 Sitemesh 过滤 --> | |
<filter> | |
<filter-name>sitemesh</filter-name> | |
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>sitemesh</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
<!-- 配置 Spring mvc 分发器 --> | |
<servlet> | |
<servlet-name>dispatcher</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
<init-param> | |
<param-name>contextClass</param-name> | |
<param-value> | |
org.springframework.web.context.support.AnnotationConfigWebApplicationContext | |
</param-value> | |
</init-param> | |
<init-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>cn.datafox.web.config.MvcConfig</param-value> | |
</init-param> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>dispatcher</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
</web-app> | |
* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment