Reputation: 163
I am creating the web application with spring 4.0.1 framework and glassfish server via NetBeans and facing problem while accessing it css and js file which is placed in project structure like below
As you can see that I have placed the Web-INF and resources at the same level(Outside from the Web-INF folder) and after trying many solutions of a similar question on stack overflow(can't share all my finding here), nothing has worked for me successfully.
Here are some changes that I have done in following files
Dispatcher-servlet.xml
<context:component-scan base-package="com.onlinetutorialspoint.controllers" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**"
location="resources/"
cache-period="31556926" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Login.jsp (used three ways to access CSS in given jsp but nothing work for me)
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/layoutlocal.css" />
<link rel="stylesheet" href="/resources/css/layoutlocal.css" />
<link href="<c:url value="/resources/css/layoutlocal.css" />" rel="stylesheet" type="text/css" />
I can't able to access any CSS or JS file even after controller and other function working fine .. but if I open the following URL in browser http://localhost:8080/SampleSpringApplication/resources/css/loginpagestyle.css, it's showing my default page instead of CSS page.
Upvotes: 2
Views: 1170
Reputation: 1233
When you are referencing the files in the jsp you need to give a relative path. that is, you need to step back from the jsp folder first and then down to the js and css files. kinda like:
<link rel="stylesheet" href="../resources/css/layoutlocal.css" />
with the ../
I think it could work.
Upvotes: 2