1

I'm unable to load js and css files in JSP file. In desktop it runs as http://localhost/ but in server it will run as http://localhost/myapp/. How can I ensure it works everywhere?

enter image description here

GET http://localhost:8080/js/jquery.min.js net::ERR_ABORTED 404 (Not Found)

GET http://localhost:8080/css/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)

In head Head tag I have this..

                <script src="/js/jquery.min.js" type="text/javascript"></script>
                <link rel="stylesheet" type="text/css"  href="/css/bootstrap.min.css">

Edit: I tried the following and it's still not working..

enter image description here

Edit: even tried the following as suggested in this article

enter image description here

8
  • It seems the configured docroot folder does not point to path webapp/WEB-INF/pages. Either you move both files into the configured docroot folder or you change the url in jsp file. Commented Oct 4, 2021 at 13:29
  • which is the docroot folder in this case? index.jsp loads fine from WEB-INF/pages Commented Oct 4, 2021 at 13:38
  • Does http://localhost:8080/js/jquery.min.js work? In my eyes the error message is different from existing source code. Are sure that is only one page that uses this include? Commented Oct 4, 2021 at 13:53
  • No it doesn't work. BTW I added the css and js to src/main/resources/static/ but it's still not working Commented Oct 4, 2021 at 13:57
  • As I wrote in my eyes the error message does not match with your code. Commented Oct 4, 2021 at 14:05

1 Answer 1

1

Fixed it.

I moved the css and js folder to src/main/resoucres/static/ and also I had to remove @EnableMVC from the Application.java and then it started working.

Also I had to add {pageContext.request.contextPath} to index.jsp otherwise it wouldn't work on the server since the URL would be http://localhost/myapp/.

<script src="{pageContext.request.contextPath}/js/jquery.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css"  href="{pageContext.request.contextPath}/css/bootstrap.min.css">
Sign up to request clarification or add additional context in comments.

Comments

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.