2

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

Spring project structure

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.

3
  • Not sure if it will work, but try to change resources/css to resources/static/css in your file structure (so add an extra folder). It helped me out before Commented May 8, 2018 at 12:21
  • @Codeer Not working Commented May 8, 2018 at 12:27
  • Hmm, then I wouldn't know, sorry! It's hard for us to reproduce your problem. Commented May 8, 2018 at 12:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Not working with absolute path. I have given the reference in jsp page like this <link rel="stylesheet" href="../resources/static/css/layoutlocal.css" />
I can't see that you have a folder called static. remove that form the path.
I have missed it but later I changed it as per your solution <link rel="stylesheet" href="../resources/css/layoutlocal.css" /> . But it doesn't working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.