Springmvc fails when the Controller jumps to the page

Bug set (1 learning): Springmvc fails when the Controller jumps to the page

Type of error

1. [WARNING] No mapping for GET /toJumpPage means that the page cannot be found, and a 404 error is also reported when the browser accesses it.
Error code:
insert image description here
insert image description here
error reason:

1. We can see that the path "/Jump" is added above this class, so when we start the server to access this resource, the url used should be: http://localhost/Jump/toJumpPage.
2. At this time, we analyze the breakpoint of this toJumpPage method. When we visit this http://localhost/Jump/toJumpPage, we will enter the method, print the "jump page" in the background, and then springmvc will return this by default. The value is regarded as a resource, and the location of the resource is searched. If it is not found, an error will be reported: "[WARNING] No mapping for GET /toJumpPage."
3. Then why do we report an error here? It is because we added a path on the class, and we must access the path under this class at localhost plus the path of Jump above the splicing method. However, when I make a jump, I will look for this resource under this classpath.
4. After we access this method, access to this resource should be like this: http://localhost/page.jsp, because I set the virtual path to / in Tomcat, this page.jsp will be directly accessed by tomcat, according to The method of http://localhost/ + page.jsp is spliced ​​together, but in fact the path we are in should be /Jump/page.jsp, so this resource cannot be found.

2. The @ResponseBody annotation is added, and the return value is returned directly as the response body.
insert image description here
insert image description here
Reason for error:
If there is no @ResponseBody, springmvc will automatically search for a resource file with the name of the return value. If it is added, it will not search for resource files, but actually transmit this string to the browser.

solution

1. The page cannot be found
1. Delete the RequestMapping annotation on the class. In this way, it can be accessed through http://localhost/page.jsp.
2. I think there are other solutions, this one is just for here, let me search first.
insert image description here

2. Instead of looking for resources, directly return the name of the resource on the page.
Get rid of @ResponseBody. will find resources.

Update: The first one can't find the page, and sure enough there is a second solution!
Sure enough, I always feel that the first solution is not realistic. What if the project has to do this? Do I have to add a path to the class? Let's take a look at one of the many methods:
the reason for this situation is actually: when we configure springmvc, we write
insert image description herethis sentence in the configuration class to indicate that the path below localhost will be intercepted by springmvc and allocated by springmvc. Steal Tomcat's work. In principle, this situation will not occur when we do not use Tomcat.
But because springmvc can simplify the development of Tomcat, we have to use springmvc, so we have other ways:
write a SpringMvcSupport class configuration under the config package, directly inherit the protected void addResourceHandlers(ResourceHandlerRegistry registry) method in the rewrite of the WebMvcConfigurationSupport class, inside Write registry.addResourceHandler("/folder/**(file resource)") .addResourceLocations("/folder/"); so that the mode of "folder" is directly matched first, which can not be intercepted by springmvc , but let tomcat allocate resources to him. The code is as follows:
insert image description hereOf course, after writing, you need to add the package scanning path to the springmvcconfig class configuration file.
insert image description here
That's it! If there is something wrong, please pay more questions! Bolt Q!

Guess you like

Origin blog.csdn.net/qq_44627822/article/details/126370159