2017-06-12 12 views
0

私は、Spring MVCのxml configをJava configに変更しようとしています。しかし、私の単純な例は機能していません。サーバー上でこのプロジェクトを実行すると、コンソール上でBeanの初期化やディスパッチャーサーブレット名を見ることができません。 と私はMavenプロジェクトを作成し、以下のいるhttp://localhost:8080/Servlet3Example/Spring MVC Java Configの問題

を実行する上で404エラーを取得するには、私のコードです:

package com.project.config; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class MyDispatcherServlet extends  AbstractAnnotationConfigDispatcherServletInitializer { 

@Override 
protected Class<?>[] getRootConfigClasses() { 
    System.out.println("get root config"); 
    //return new Class[]{RootConfig.class}; 
    return null; 
} 

@Override 
protected Class<?>[] getServletConfigClasses() { 
    System.out.println("get web config"); 
    return new Class[]{WebConfig.class}; 
} 

@Override 
protected String[] getServletMappings() { 
    System.out.println("in dispatcher servlet"); 
    return new String[] {"/"}; 
} 

}

とのWebConfigです:

package com.project.config; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.ViewResolver; 
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages={"com.project.controllers"}) 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Bean 
public ViewResolver viewResolver() { 
    InternalResourceViewResolver viewResolver = new  InternalResourceViewResolver(); 
    viewResolver.setSuffix(".jsp"); 
    viewResolver.setPrefix("/WEB-INF/views/"); 
    return viewResolver; 
} 

@Override 
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
    configurer.enable(); 
} 

}

コントローラ:このすべては、私には罰金を見ているようだ@ComponentScan

+0

@ComponentScan(basePackages={"com.project.*"})

package com.project.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value="/",method=RequestMethod.GET) public String home() { return "home"; } } 
Plog

+0

'http:// localhost:8080 /' – jmw5598

+0

いいえ、MyDispatcherServletが初期化されていないようです。 ConfigもコンソールにBeanのリファレンスはありません。コンソールにエラーはありません。 – Megha

答えて

0
あなたが変更してくださいことができます。サーバーログにエラーが発生していますか?また、MyDispatcherServletとMyConfigにいくつかのブレークポイントを入れて、これらが初期化されているかどうかを確認してください。
関連する問題