Package de.codecentric.moviedatabase.config

Source Code of de.codecentric.moviedatabase.config.ApplicationInitializer

package de.codecentric.moviedatabase.config;

import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

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.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;

public class ApplicationInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(ApplicationConfiguration.class);
    rootContext.setDisplayName("Movie database");
    servletContext.addListener(new ContextLoaderListener(rootContext));
    ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
   
    Dynamic filter = servletContext.addFilter("characterEncodingFilter", CharacterEncodingFilter.class);
    filter.addMappingForUrlPatterns(null, false, "/*");
    filter.setInitParameter("encoding", "UTF-8");
    filter.setInitParameter("forceEncoding", "true");
   
    servletContext.addFilter("corsFilter", CorsFilter.class)
      .addMappingForUrlPatterns(null, false, "/*");
   
    servletContext.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class)
      .addMappingForUrlPatterns(null, false, "/*");
  }
}
 
TOP

Related Classes of de.codecentric.moviedatabase.config.ApplicationInitializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.