2017-08-30 122 views
0

我使用弹簧配置(不带xml)的弹簧mvc。 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; 
} 

}

+0

您的MainController在com.chat包下? –

+0

com.chat.controller - mainController; com.chat.config - webmvcConfig –

+0

更改映射从/像这样开始'@RequestMapping(value =“/ welcome”,method = RequestMethod.GET)' – StanislavL

回答

1

所以..我解决的问题。它在Controller - 路径中。我的想法自动改变从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?