简体   繁体   中英

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.

Here's the console 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
@RequestMapping("/socios")
public class SocioController {
    
    @GetMapping("/showSocios")
    public String showSocios() {
        return "socios";
    }
}

The ViewResolver bean in @Configuration class:

@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. 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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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