简体   繁体   English

Spring MVC 在查找 JSP 个文件时抛出 404 NOT_FOUND

[英]Spring MVC throws 404 NOT_FOUND while looking for JSP files

In a simple Spring Core MVC Project (No Spring Boot, no XML configuration) I got 404 NOT_FOUND while trying to access to a URL declared in a controller, and it says that cannot find the JSP File.在一个简单的 Spring 核心 MVC 项目(没有 Spring 启动,没有 XML 配置)中,我在尝试访问 controller 中声明的 URL 时得到 404 NOT_FOUND,它说找不到 8820427489022 文件

Here's the console output:这是控制台 output:

[http-nio-8080-exec-1] DEBUG DispatcherServlet - GET "/appmutualista/socios/showSocios", parameters={}
[http-nio-8080-exec-1] DEBUG RequestMappingHandlerMapping - Mapped to dev.lorena.projects.appmutualista.controller.SocioController#showSocios()
[http-nio-8080-exec-1] DEBUG JstlView - View name 'socios', model {}
[http-nio-8080-exec-1] DEBUG JstlView - Forwarding to [/webapp/view/socios.jsp]
[http-nio-8080-exec-1] DEBUG DispatcherServlet - Completed 404 NOT_FOUND

The controller: controller:

@Controller
@RequestMapping("/socios")
public class SocioController {
    
    @GetMapping("/showSocios")
    public String showSocios() {
        return "socios";
    }
}

The ViewResolver bean in @Configuration class: @Configuration class 中的 ViewResolver bean:

@Bean
public ViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
    internalResourceViewResolver.setPrefix("/webapp/view/");
    internalResourceViewResolver.setSuffix(".jsp");
    return internalResourceViewResolver;
 }

And the project structure:和项目结构:

项目结构

I am using embedded Tomcat 9 and JDK 8 to run the APP.我正在使用嵌入式 Tomcat 9 和 JDK 8 来运行该应用程序。 After hours, I cannot figure out what I am doing wrong.几个小时后,我无法弄清楚我做错了什么。

Usually project has next hierarhy:通常项目有下一个层次结构:

src
     `-- main
         |-- java
         |   `-- ...
         |-- resources
         |   `-- ...
         `-- webapp
             |-- ...

And it is better to make same hierarhy in your code.最好在您的代码中创建相同的层次结构。 In this case root is webapp folder for InternalResourceViewResolver在这种情况下,root 是InternalResourceViewResolverwebapp文件夹

internalResourceViewResolver.setPrefix("/view/")

Jsp should be in webapp hierarhy, due to different behaviour in case of store you jsp in resources folder (war and jar package store this jsp in different places in builder war and jar). Jsp 应该在webapp层次结构中,由于将 jsp 存储在资源文件夹中的行为不同(war 和 jar package 将此 jsp 存储在构建器 war 和 jar 的不同位置)。

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

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