简体   繁体   English

spring mvc no xml 配置404错误

[英]spring mvc no xml configuration 404 error

I am trying to develop a simple Spring MVC with no XML application.its basically show a simple home page.我正在尝试开发一个没有 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/ 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/

this is a controller这是 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[]{"/"};
    }
}

this is error log这是错误日志

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'

this is also how my project structure这也是我的项目结构

在此处输入图像描述

You need to define a resource path if you adding something into your URL (after host part basically in your case MySpringSecurityApp_war ) you are calling localhost:8080/MySpringSecurityApp_war/ but you didn't define the resource path anywhere so I guess what you need to do is either add @RequestMapping("/MySpringSecurityApp_war/") at class level or just call localhost:8080/ without any resource path You can also use @RestController in place of @Component .如果您在 URL 中添加一些东西(在您的情况下基本上是主机部分MySpringSecurityApp_war之后),您需要定义一个资源路径,您正在调用localhost:8080/MySpringSecurityApp_war/但您没有在任何地方定义资源路径所以我猜你需要什么要做的是在 class 级别添加@RequestMapping("/MySpringSecurityApp_war/")或者只调用localhost:8080/而不使用任何资源路径您也可以使用@RestController代替@Component

I hope it will work.我希望它会奏效。

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

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