Deploy the springboot war package to tomcat, static resources, bootstrap, etc. URLs, as well as the unavailability of the mapping mapping of the controller, etc.

Recently, when deploying a springboot war file to tomcat, there were a lot of 404, not found problems, which did not appear in eclipse debugging.
After careful verification, it turned out that there was indeed a problem with the path.
You can see his background access path in the browser as follows:

XHR POST http://127.0.0.1:8080/home/welcome                    //这是一个mapping映射,到controller的
XHR POST http://127.0.0.1:8080/home/getTree     //这是一个对静态资源的访问

There is nothing wrong with this path, it is exactly the same as the path when I debugged it in eclipse, but a 404 error was reported.
The reason is, how can it be exactly the same? Of course it is wrong.
In fact, any more complete and reliable tutorial on the Internet that teaches you how to generate war package, there will be such a sentence at the end,
http://localhost:[port number]/[打Project name]/
because of war package Running in tomcat is not a war package that runs directly. The first step is that tomcat will automatically decompress the war package into a folder with the same name as the war package. All your class files and various static files are in it.
Therefore, when logging in through the web page, of course, you should add the name of your project, that is, the name of your folder.
When I logged in to my homepage, I noticed that the project name was added, but when I visited the catalog later, I saw a red color. I took it for granted. I neglected it. I didn't expect it.
To access all the contents of this folder, including static files and how to map and other files related to the path, you need to add this folder name! .
Therefore, his correct access directory should be:

XHR POST http://127.0.0.1:8080/myproject/home/welcome                    //这是一个mapping映射,到controller的
XHR POST http://127.0.0.1:8080/myproject/home/getTree     //这是一个对静态资源的访问

To solve this problem, I referred to several methods.
1. I used thymeleaf's th:src="@{XXXXX}" to cite my static files.
2. Copy all the contents of the project folder I deployed to the ROOT folder under tomcat's webapps. Note that without the project folder, it is all the contents under the root directory of the folder.
3. In the configuration file, configure server.servlet.context-path=/myproject. This will add a unified prefix to the mapping in all controllers and static files. However, it should be noted that this will not automatically be html Add a prefix to the request path of the access mapping or the referenced path, or you need to modify it yourself. It is more convenient to add... in front of your mapping path, for example, my mapping path bit /net/loadData, modified to.../net/loadData, relatively speaking once and for all.
4. In fact, there is no need to configure server.servlet.context-path=/myproject, just add .../ to the path location, such as .../net/loadData, this bug will not appear. Of course, there is no problem with it.

Guess you like

Origin blog.csdn.net/baidu_31788709/article/details/104410720
Recommended