From d34b48138c7f4ad24fff1aacc14041aaeaf86a7e Mon Sep 17 00:00:00 2001 From: Li Shengzhao Date: Sun, 3 Apr 2016 22:54:54 +0800 Subject: [PATCH] =?UTF-8?q?(118)=20-=20Add=20java-config(=E9=9B=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE)=20=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sos/config/ServletInitializer.java | 67 +++++++++++++++++++ .../webapp/WEB-INF/{web.xml => web.xml.old} | 0 2 files changed, 67 insertions(+) create mode 100644 src/main/java/com/monkeyk/sos/config/ServletInitializer.java rename src/main/webapp/WEB-INF/{web.xml => web.xml.old} (100%) diff --git a/src/main/java/com/monkeyk/sos/config/ServletInitializer.java b/src/main/java/com/monkeyk/sos/config/ServletInitializer.java new file mode 100644 index 0000000..f73512f --- /dev/null +++ b/src/main/java/com/monkeyk/sos/config/ServletInitializer.java @@ -0,0 +1,67 @@ +package com.monkeyk.sos.config; + +import com.monkeyk.sos.web.filter.CharacterEncodingIPFilter; +import com.opensymphony.sitemesh.webapp.SiteMeshFilter; +import org.springframework.web.filter.DelegatingFilterProxy; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +import org.springframework.web.util.Log4jConfigListener; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +/** + * 2016/4/3 + *

+ * Config DispatchServlet + *

+ * Replace web.xml + * + * @author Shengzhao Li + */ +public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + + @Override + protected Class[] getRootConfigClasses() { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + protected Class[] getServletConfigClasses() { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } + + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + super.onStartup(servletContext); + +// servletContext.setAttribute("webAppRootKey", "spring-oauth-server"); + servletContext.setInitParameter("webAppRootKey", "spring-oauth-server"); + servletContext.setInitParameter("contextConfigLocation", "classpath:spring/*.xml"); + servletContext.setInitParameter("log4jConfigLocation", "/WEB-INF/log4j.xml"); + + //Add Filters + + CharacterEncodingIPFilter characterEncodingIPFilter = new CharacterEncodingIPFilter(); + characterEncodingIPFilter.setEncoding("UTF-8"); + characterEncodingIPFilter.setForceEncoding(true); + servletContext.addFilter("encodingFilter", characterEncodingIPFilter).addMappingForUrlPatterns(null, false, "/*"); + + DelegatingFilterProxy securityFilter = new DelegatingFilterProxy("springSecurityFilterChain"); + servletContext.addFilter("springSecurityFilterChain", securityFilter).addMappingForUrlPatterns(null, false, "/*"); + + SiteMeshFilter siteMeshFilter = new SiteMeshFilter(); + servletContext.addFilter("sitemesh", siteMeshFilter).addMappingForUrlPatterns(null, false, "/*"); + + //Add Listeners + + servletContext.addListener(Log4jConfigListener.class); + + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml.old similarity index 100% rename from src/main/webapp/WEB-INF/web.xml rename to src/main/webapp/WEB-INF/web.xml.old