2016-03-20 27 views
1

varsayılan sunucu adını bulamadığı için web.xml ve spring-servlet.xml config dosyası olmadan basit bir uygulama oluşturdum. Sadece Java kodunda yapıyorum. Bu proje yapısı şöyledir:Jetty + SpringMVC: configureDefaultServletHandling ayarlandığında hata,

Project structure

Bu benim ana sınıftır:

@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<HttpMessageConverter<?>> 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; 
} 
} 
: Bahar-servlet için yapılandırma ayarlamak için WebConfig.java adında başka bir sınıf oluşturdu
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 > 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; 
} 
} 

configureDefaultServletHandling işlevini eklediğimde hata oluştu.

[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. 

Varsayılan sunucu adı adı bulamıyor. Normalde, varsayılan sunucu adı "varsayılan" olacaktır.

Belki de bu sorunu çözmek için sunucu adını ayarlamalıyım.

Bu sorunu çözmek için herhangi bir fikriniz var mı?

Kod formatı düzgün görüntülenemedi. Desteklediğiniz için teşekkür ederiz. Çok teşekkür ederim.

cevap

0

Zaten java koduna göre Bahar servlet adını nasıl ayarlanacağı bulundu:

contextHandler.addServlet(new ServletHolder("default", new DispatcherServlet(context)), MAPPING_URL); 

Ama başka bir sorunu tanıştım, yani:

Ben google deneyin
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.util.NestedServletException 

, birçok kişi karşı karşıya NoClassDefFoundError ile ancak hiç kimse org.springframework.web.util.NestedServletException numaralı sorunu karşılamamıştır.

Tüm bağımlılığın eklendiğinden emin olmak için tekrar kontrol ettim.

Belki bu konuda :(başka bir sorum gönderilmesi gerekiyor

+0

bir NoClassDefFoundError tipik tipik Bahar MVC ve konteyner varsayılan servlet arasında, bir StackOverflow bir sonucu olarak gerçekleşir Böyle – bla