繁体   English   中英

控制器不适用于Spring

[英]Controller doesn't work with spring

我将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;
    }
}

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

另外,我找到了下一篇关于类似麻烦的文章。 可能会有所帮助! 如何将Spring MVC控制器映射到带有和不带有斜杠的uri?

暂无
暂无

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

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