arash moeen arash moeen - 1 year ago 75
HTML Question

Spring dynamic url access to css/js files

I have a simple Spring boot application. There URL that would be used here is going to be like:

and variable1 and variable2 can be anything for example:

and I capture those in my controller as below:

public class IndexController {

@RequestMapping(value="/{var1}/{var2}", method = RequestMethod.GET)
public String Index(@PathVariable(value="var1") String variable1, @PathVariable(value="var2") String variable2) {
return "/index.html";

which will redirect to my html page which is located at

so the index page is loaded under the dynamic url but then none of the scrips or stylesheets are found because it's trying to find them under variable1 path.

for example if I have

and my link to css in the index.html is

<link rel="stylesheet" href="styles/index.css" />

it will try to find the index.css under

how can I tell the app to look into the right place for the styles or js files?

Answer Source

index.html is trying to load styles from a relative path.

In order for index.html to load styles under, add a / in link tag **/**styles/index.css <link rel="stylesheet" href="/styles/index.css" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download