Skip to content

Instantly share code, notes, and snippets.

@jbrackett
Last active August 29, 2015 13:56
Show Gist options
  • Save jbrackett/8909259 to your computer and use it in GitHub Desktop.
Save jbrackett/8909259 to your computer and use it in GitHub Desktop.
HelloWorldSpring4Configuration
package com.hello;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = { "com.hello.controller", "com.hello.config" })
public class AppConfig {
}
package com.hello.config;
import javax.servlet.Filter;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.hello.AppConfig;
import com.hello.WebConfig;
public class ServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setInitParameter("dispatchOptionsRequest", "true");
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter charFilter = new CharacterEncodingFilter();
charFilter.setEncoding("UTF-8");
charFilter.setForceEncoding(true);
return new Filter[] { new HiddenHttpMethodFilter(), charFilter,
new HttpPutFormContentFilter() };
}
}
package com.hello;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.hello.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment