Omkar Shetkar
Omkar Shetkar

Reputation: 3636

Spring MVC not picking static resources mentioned in html files

I am developing a simple Spring MVC(v4.1.2) and Angular4 app.

Basically this app does CRUD operations by making http requests from angular client.

Following combination works perfectly fine: Angular app ran using "ng serv" Spring MVC war deployed in a application server.

Now, I am trying to combine both the client and server into a single project. With this I should be able to generate a single war file containg both client and server side code.

For this,

After deploying and running the app, I get following errors in the web console:

enter image description here

index.html

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>StoreClientApp</title>
      <base href="/">

      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    </head>
    <body>
      <app-root></app-root>
    <script type="text/javascript" src="inline.bundle.js"></script><script 
     type="text/javascript" src="polyfills.bundle.js"></script><script 
     type="text/javascript" src="styles.bundle.js"></script><script 
     type="text/javascript" src="vendor.bundle.js"></script><script 
     type="text/javascript" src="main.bundle.js"></script></body>

    </html>

It is evident from the console logs that Spring is triggering http requests for the static resources in index.html. Is this expected behaviour? What should be the changes so that Spring considers static resources as actually static and fetches them from relative path?

Thanks.

Upvotes: 1

Views: 395

Answers (3)

TonyY
TonyY

Reputation: 703

http://127.0.0.1:7001/inline.bundle.js It hasn't contextPath, so you need correct it.

http://127.0.0.1:7001/store-server/inline.bundle.js

1.static definition

<base href="/store-server">

2.dynamic definition

If you use pure HTML, you can get contextPath by the following.

<script type="text/javascript">
    var contextPath = "/"+window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
    document.head.innerHTML = document.head.innerHTML + "<base href='" + contextPath + "' />";
</script>

Upvotes: 2

AchillesVan
AchillesVan

Reputation: 4356

In your index.html Try to add a forward slash to each one of the URLs of the script file being used . Like this

 <script type="text/javascript" src="/inline.bundle.js"></script><script 
type="text/javascript" src="/polyfills.bundle.js"></script><script
 type="text/javascript" src="/styles.bundle.js"></script><script 
type="text/javascript" src="/vendor.bundle.js"></script><script 
type="text/javascript" src="/main.bundle.js"></script></body>

Upvotes: 0

GSSwain
GSSwain

Reputation: 6133

Your context path is /store-server and the requests to your static resources are to the root. You can do one of the following to fix it

  • Update the <base href="/"> to <base href="/store-server">
  • Deploy your spring war at the root context path(/)
  • Update the path of the individual script/css to point to /store-server/{resource-name}

Upvotes: 1

Related Questions