[英]spring mvc no xml configuration 404 error
我正在嘗試開發一個沒有 XML 應用程序的簡單 Spring MVC。它基本上顯示一個簡單的主頁。 I am using tomcat on JetBrains IDE for development and problem is that when I run it on tomcat I see 404 error this is url http://localhost:8080/MySpringSecurityApp_war/
這是 controller
@Component
public class DemoController {
@GetMapping("/")
public String showHome(){
return "home";
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.luv2code.springsecurity.demo")
public class DemoAppConfig {
//define a bean for view resolver
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {DemoAppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
這是錯誤日志
9-Jun-2020 13:32:07.511 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/MySpringSecurityApp_war/] in DispatcherServlet with name 'dispatcher'
09-Jun-2020 13:32:07.604 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/MySpringSecurityApp_war/] in DispatcherServlet with name 'dispatcher'
這也是我的項目結構
如果您在 URL 中添加一些東西(在您的情況下基本上是主機部分MySpringSecurityApp_war
之后),您需要定義一個資源路徑,您正在調用localhost:8080/MySpringSecurityApp_war/
但您沒有在任何地方定義資源路徑所以我猜你需要什么要做的是在 class 級別添加@RequestMapping("/MySpringSecurityApp_war/")
或者只調用localhost:8080/
而不使用任何資源路徑您也可以使用@RestController
代替@Component
。
我希望它會奏效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.