gpt4 book ai didi

java - Controller 不支持 Spring

转载 作者:行者123 更新时间:2023-12-02 12:18:44 26 4
gpt4 key购买 nike

我使用 spring mvc 和 spring 配置(没有 xml)。而且 IDEA 似乎不会进入 Controller 代码。也许某处路径不正确,因此 @RequestMapping 不起作用。但我无法理解具体在哪里。这是我的 Controller

@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 - 路径中。我的想法自动将路径从 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?

关于java - Controller 不支持 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45959263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com