Java SpringMVC控制器不';行不通

Java SpringMVC控制器不';行不通,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在学习SpringMVC并尝试构建HelloWorld Web应用程序。 我使用第四版SpingInAction4th中的代码用eclipse构建这个项目, 但是当我通过访问在我的浏览器上测试它时,我得到了404错误。 最奇怪的是,如果我使用MockMvc(Spring在Action中提供的方法)测试控制器,它将通过测试。 所以我想知道我哪里做错了 我的项目结构: SpitrWebAppInitializer.java: package spittr.config; import or

我正在学习SpringMVC并尝试构建HelloWorld Web应用程序。
我使用第四版SpingInAction4th中的代码用eclipse构建这个项目,
但是当我通过访问
在我的浏览器上测试它时,我得到了404错误。


最奇怪的是,如果我使用MockMvc(Spring在Action中提供的方法)测试控制器,它将通过测试。
所以我想知道我哪里做错了

我的项目结构:

SpitrWebAppInitializer.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[]{"/"};
    }

}

RootConfig.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();
    }

}
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";
    }

}
您的包名是spittr.web

@ComponentScan(basePackages={"spitter"}
这里也一样

添加说明:

@ComponentScan正在提供的包及其所有子包上寻找@Component(包括@Repository、@Service和@Controller)注释类,以便将它们添加到Spring上下文中。虽然提供的包不存在,但Spring找不到您的控制器,因此它不会创建它

当您测试它时,它会工作,因为您在测试中显式地使用它

您的包名是spittr.web

@ComponentScan(basePackages={"spitter"}
这里也一样

添加说明:

@ComponentScan正在提供的包及其所有子包上寻找@Component(包括@Repository、@Service和@Controller)注释类,以便将它们添加到Spring上下文中。虽然提供的包不存在,但Spring找不到您的控制器,因此它不会创建它

当您测试它时,它会起作用,因为您在测试中明确使用它。

我太粗心了……:(谢谢!我太粗心了……:(谢谢!