0

I have a Spring boot application that works correctly. I created a war file (MyApp.war) following the instructions available from spring guide.

I have installed Tomcat 8.5.24 on Ubuntu desktop and it is running successfully.

If I delete the out of the box ROOT application from /opt/tomcat/webapps/ and then copy MyApp.war as /opt/tomcat/webapps/ROOT.war then my spring boot application works correctly and the static files are returned back to the browser successfully by tomcat server. No issues.

My goal is tomcat's out of the box ROOT application must continue to work as is and static files starting with the string /assets specified in the index.html in MyApp.war must be processed successfully.

So, I uninstalled and reinstalled tomcat successfully on my ubuntu desktop to verify the out of the box tomcat's ROOT app is working correctly.

After reading tomcat documentation as well as stack overflow favorite Q&A's related to context.xml, I tried the following:

I changed context.xml as shown below:

$ diff /opt/tomcat/conf/context.xml /opt/tomcat/conf/context.xmlOriginal
19c19
< <Context docBase="MyApp"   path="/assets"   reloadable="true">
---
> <Context>

The intent of the above change in context.xml, is when the browser request for static files using the URL like bootstrap then I want tomcat to fetch the file from the URL new bootstrap , that is a redirection needs to take place.

After making the above change I restarted tomcat and copied MyApp.war to webapps directory. When I go to the URL MyAppUrl ,from Chrome developer tools network tab, I can see index.html got loaded correctly, as shown below:

Request URL:http://localhost:8080/MyApp/
Request Method:GET
Status Code:200
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade

In the response tab, I see index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>MyApp</title>
      <link rel="stylesheet" type="text/css" href="/assets/css/bootstrap.css">
...

However, bootstrap.css did not load correctly. Here is the error, from network tab that clearly says that the browser asked for bootstrap.css and got 404 response back.

Request URL:http://localhost:8080/assets/css/bootstrap.css
Request Method:GET
Status Code:404
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade

I can verify bootstrap.css can be successfully retrieved if I go to the URL success bootstrap URL .

My question is: what change is needed in /opt/tomcat/conf/context.xml for tomcat 8 to return bootstrap.css from the URL redirectedURL whenever the browser asks for a static resource with a request URL ? Thanks a lot,

Ganesh
  • 44
  • 1
  • 6

1 Answers1

0

working code: use thymleaf then declare url like =

<link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.css}" />

i think your problem will solve