简体   繁体   English

SpringMVC控制器不起作用

[英]SpringMVC Controller doesn't work

I am learning SpringMVC and try to build a HelloWorld webapp. 我正在学习SpringMVC,并尝试构建一个HelloWorld Webapp。
I build this project with eclipse using the code from Sping in Action 4th Edition , 我使用Sping in Action 4th Edition中的代码用eclipse构建了这个项目,
but when I test it on my browser by visiting http://localhost:8080/homepage 但是当我通过访问http:// localhost:8080 / homepage在浏览器上对其进行测试时
I got 404 error. 我收到404错误。
在此处输入图片说明

And the most weired thing is, if I test the controller using MockMvc (method provided by Spring in Action , it will pass the test. 最奇怪的是,如果我使用MockMvc(由Spring in Action提供的方法)测试控制器,它将通过测试。
SO I am wondering where did I do wrong? 所以我想知道我在哪里做错了?

The structure of my project: 我的项目结构:
在此处输入图片说明

SpittrWebAppInitializer.java: 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 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 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 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";
    }

}
@ComponentScan("spitter.web")

Your package name is spittr.web 您的软件包名称是spittr.web

@ComponentScan(basePackages={"spitter"}

Same here 同样在这里

ADDED EXPLANATION: 附加说明:

@ComponentScan is looking for @Component (including @Repository, @Service and @Controller) annotated classes on the provided package and all its subpackages in order to add them to the Spring Context. @ComponentScan在提供的包及其所有子包上寻找@Component(包括@ Repository,@ Service和@Controller)注释的类,以便将它们添加到Spring Context中。 While the provided packages don't exist, Spring doesn't find your controller so it doesn't create it. 虽然提供的软件包不存在,但Spring找不到您的控制器,因此不会创建它。

It works when you test it because you are using it explicitly on your tests. 当您测试它时它可以工作,因为您在测试中明确使用了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM