2016-12-26 62 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