2016-03-20 11 views
1

web.xmlとspring-servlet.xml設定ファイルを使用せずに単純なアプリケーションを作成しました。私はちょうどJavaコードを設定します。 これは、プロジェクト構造である:Jetty + SpringMVC:デフォルトのサーブレット名が見つからないため、configureDefaultServletHandlingを設定するとエラーが発生する

Project structure

これは私のメインクラスです:

@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; 
} 
} 
:私は春・サーブレットの設定を設定するWebConfig.javaという名前の別のクラスを作成し
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機能を追加するとエラーが発生しました。

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

サーブレットのデフォルト名が見つかりません。通常、サーブレットのデフォルト名は "default"になります。

この問題を解決するには、サーブレット名を設定する必要があります。

この問題を解決するにはどうすればよいですか?

コードフォーマットが正しく表示されません。あなたに感謝しています。どうもありがとうございました。

答えて

0

私はすでにJavaコードで春のサーブレットの名前を設定する方法が見つかりました:

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

しかし、私は別の問題に会った、それは次のようになります。私は多くの人々が直面した、グーグルしよう

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

NoClassDefFoundErrorがありますが、誰も問題を満たしていませんでしたorg.springframework.web.util.NestedServletException

すべての依存関係が追加されたことを再度確認しました。

たぶん私はその問題:(ために別の質問を投稿する必要があり

+0

のNoClassDefFoundErrorが一般的に通常のSpring MVCとコンテナのデフォルトのサーブレット間、StackOverflowのの結果として起こるような – bla

関連する問題