控制器不适用于弹簧

时间:2017-08-30 11:31:24

标签: java spring controller

我使用spring mvc和spring配置(没有xml)。似乎IDEA没有进入控制器代码。也许某处路径不正确所以@RequestMapping不起作用。但我无法理解究竟在哪里。 这是我的控制器

@Controller
public class MainController {

    @RequestMapping(value = "/" , method = RequestMethod.GET)
    public String home() {

        return "index";
    }
    @RequestMapping(value = "welcome", method = RequestMethod.GET)
    public String welcome(Model m){
        m.addAttribute("name","lol kkeke");
        return "index2";
    }
}

WebMvcConfig

@Configuration
@ComponentScan("com.chat")
@EnableWebMvc
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
        registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
        registry.addResourceHandler("/pages/**").addResourceLocations("/views/");

    }

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

    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/index.jsp");
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

1 个答案:

答案 0 :(得分:1)

所以..我解决了一个问题。它在控制器 - 路径中。我的想法自动改变从com.chat.controller到c.c.controller的路径。所以我将项目结构重建为com.chat.controller.Controller.class;和com.chat.config.Configuration.class。

另外,我发现了下一篇关于类似问题的文章。可能它会帮助别人! How do I map Spring MVC controller to a uri with and without trailing slash?