2016-12-26 97 views
0

我正在學習SpringMVC並嘗試構建HelloWorld Web應用程序。
我建立使用的代碼項目使用Eclipse在行動第四版,
自旋微觀但是當我訪問http://localhost:8080/homepage
測試它在我的瀏覽器我得到404錯誤。
enter image description here

而最weired的事情是,如果我使用MockMvc通過春天在行動提供的(方法,它將通過測試測試控制器。
所以我想知道在哪裏我做錯了什麼?
SpringMVC控制器不起作用

我的項目結構:
enter image description here

SpittrWebAppInitializer.java:

package spittr.config; 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     // TODO Auto-generated method stub 
     return new Class<?>[] {RootConfig.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     // TODO Auto-generated method stub 
     return new Class<?>[] {WebConfig.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     // TODO Auto-generated method stub 
     return new String[]{"/"}; 
    } 

} 


WebConfig.java

package spittr.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("spitter.web") 
public class WebConfig extends WebMvcConfigurerAdapter { 

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

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

} 


RootConfig.java

package spittr.config; 

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.ComponentScan.Filter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 

@Configuration 
@ComponentScan(basePackages={"spitter"},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}) 
public class RootConfig { 

} 


HomeController.java

package spittr.web; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping({"/","/homepage"}) 
public class HomeController { 

    @RequestMapping(method=RequestMethod.GET) 
    public String home(){ 
     return "home"; 
    } 

} 

回答

2
@ComponentScan("spitter.web") 

你包的名字是spittr.web

@ComponentScan(basePackages={"spitter"} 

同樣在這裏

增加內容,說明:

@ComponentScan正在尋找@Component(包括@Repository,@Service和@Controller )在提供的包及其所有子包中添加註釋類,以便將它們添加到Spring上下文中。雖然提供的軟件包不存在,但Spring並沒有找到你的控制器,所以它不會創建它。

它測試時會起作用,因爲您在測試中明確使用它。

+0

我很粗心...... :(謝謝! – TomLeung