user93796
user93796

Reputation: 18379

static content rendering with spring mvc

I am new to spring and am trying it.

I have a jsp file which includes css and images as follows

<link href="css/style.css" rel="stylesheet" type="text/css" />

Now i have a url path as /localhost/myapp/test/tst which is mapped to a controller which then forwards it to view jsp

view.jsp is present it

/web-inf/jsp/view.jsp

But when hit the path /localhost/myapp/test/tst the css and images path gets resolved to

/localhost/myapp/test/tst/css/style.css

Actaully i want it to come as /localhost/myapp/css/style.css css and html are present in root of webapp

How i have following in my spring config

 <mvc:annotation-driven />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />

Upvotes: 1

Views: 439

Answers (1)

nickdos
nickdos

Reputation: 8414

Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.

<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />

Upvotes: 2

Related Questions