1
votes

I am using Spring boot starter web, and I am trying to link my stylesheet style.css in my JSP. How can I use the default configuration to link my css correctly ?

The following line in the <head></head> isn't working:

>page.jsp:

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

Location of the files:

  • style.css : /src/main/resources/static/css/style.css
  • page.jsp : /src/main/webapp/WEB-INF/jsp/page.jsp

style.css

enter image description here

Note :I can access my CSS using this address http://localhost:8080/css/style.css

On the other hand, using a JSTL tag works :

>page.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:url value="/css/style.css" var="jstlCss" />
<link href="${jstlCss}" rel="stylesheet" >

Could someone explain me the reason?

1

1 Answers

0
votes

First you need to declare your resources in dispatcher-servlet file like this :

<mvc:resources mapping="/resources/**" location="/resources/static/" />

Any request with url mapping /resources/** will directly look for /resources/static/.

Now in jsp file you need to include your css file like this :

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

Or if you want use Jstl just like this

<link href="<c:url value="/css/style.css" />" rel="stylesheet">

Similarly you can include js files for your project.