title: Jetty + SpringMVC: Error when set configureDefaultServletHandling because cannot find the default servlet name
body: <p>I created an simple application without web.xml and spring-servlet.xml config file. I just config in Java code.
  This is the project structure:</p>
  
  <p><a href="http://i.stack.imgur.com/Hw9iu.png" rel="nofollow">Project structure</a></p>
  
  <p>This is my main class:</p>
  
  <pre><code>public class TripMapServer {
  
  private static final int DEFAULT_PORT = 8080;
  private static final String CONTEXT_PATH = "/";
  private static final String MAPPING_URL = "/";
  private static final String CONFIG_LOCATION = "com.biendltb.config";
  private static final String DEFAULT_PROFILE = "dev";
  
  public static void main(String[] args) throws Exception {
      new TripMapServer().startJetty(getPortFromArgs(args));
  }
  
  private static int getPortFromArgs(String[] args) {
      if (args.length &gt; 0) {
          try {
              return Integer.valueOf(args[0]);
          } catch (NumberFormatException ignore) {
          }
      }
      return DEFAULT_PORT;
  }
  
  private void startJetty(int port) throws Exception {
      Server server = new Server(port);
      server.setHandler(getServletContextHandler(getContext()));
      server.start();
      server.join();
  }
  
  private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
      ServletContextHandler contextHandler = new ServletContextHandler();
      contextHandler.setErrorHandler(null);
      contextHandler.setContextPath(CONTEXT_PATH);
      contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
      contextHandler.addEventListener(new ContextLoaderListener(context));
      contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
      return contextHandler;
  }
  
  private static WebApplicationContext getContext() {
      AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
      context.setConfigLocation(CONFIG_LOCATION);
      context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE);
      return context;
   }
  }
  </code></pre>
  
  <p>I created another class named WebConfig.java to set the config for spring-servlet:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = {"com.biendltb.controller"})
  public class WebConfig extends WebMvcConfigurerAdapter{ 
  
  private static final Charset UTF8 = Charset.forName("UTF-8");
  
  @Autowired
  public Environment env;
  
  @Override
  public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
      converters.add(stringConverter());
  }
  
  private StringHttpMessageConverter stringConverter() {
      StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
      stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", UTF8)));
      return stringConverter;
  }
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/src/main/resources/").setCachePeriod(Integer.MAX_VALUE);
      registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(Integer.MAX_VALUE);
      registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(Integer.MAX_VALUE);
      registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(Integer.MAX_VALUE);
  }
  
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
      configurer.enable();
  }
  
  @Bean
  public InternalResourceViewResolver getInternalResourceViewResolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      resolver.setPrefix("/src/main/resources/pages/");
      resolver.setSuffix(".jsp");
      return resolver;
   }
  }
  </code></pre>
  
  <p>Error happened when I add the <b>configureDefaultServletHandling</b> function. </p>
  
  <pre><code>[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.
  
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.
  </code></pre>
  
  <p>It cannot find the default name of servlet. Normally, the default name of servlet will be "default".</p>
  
  <p>I think maybe I should set the servlet name to solve this problem.</p>
  
  <p>Do you have any idea to solve this problem?</p>
  
  <p>Sorry about the code format not display correctly. Appreciated you support. Thank you very much.</p>