JSP - CSS file and JS files aren't loading at all
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">
JSP doesn't see CSS
There are two problems in your app:
In JSP, you should use
${pageContext.request.contextPath}
to append the base path for your URLs. With this, you can be sure you will use absolute path instead of relative path for your URLs. So, this:<link rel="stylesheet" type="text/css" href="../css/style.css"/>
Will be
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css"/>
This can also be accomplished by using
<c:url>
from JSTL:<link rel="stylesheet" type="text/css" href="<c:url value='/css/style.css' />"/>
Your servlet is mapping to every request made in your app. Note that this includes simple requests for resources like this CSS file. If you're not handling these requests successfully, you may get an error response or an empty response or whatever, depends on how you handle the request for CSS files in the servlet. I recommend you to change the URL pattern for your servlet to map to specific paths instead.
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
i can't link my css to my jsp in eclipse working on dynamic web project jee
Guys i just reinstalled Eclipse using jdk 8 then i placed my css in a folder named css inside WebContent .
and finally linked my jsp with :
<link rel="stylesheet" href="<c:url value ="/css/loginStyle.css"/>" />
Can't access CSS and JavaScript files in JSP
When you are running application then controller calls the .jsp and jsp is get converted to servlet during translation phase so try to add relative path using ${pageContext.request.contextPath}
and once check the url pattern in web.xml and once left it blank and then try it may work.
JSP doesn't see CSS and JS files from resources folder
For JSP project:
Create cssLoader.jsp page inside folder webapp
as below:
webapp
-cssLoader.jsp
cssLoader.jsp
<link rel="stylesheet" href="resources/stylesheet/common.css"/>
<link rel="stylesheet" href="resources/stylesheet/index.css"/>
<script type="text/javascript" src="resources/js/validators/loginValidator.js"></script>
Then load this cssLoader page where you want to import css/js file in page using <jsp:include page="../../cssLoader.jsp"></jsp:include>
.
For example: in my case
DIRECTORY:
WEB-INF
-pages
-page.jsp
page.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WEB-INF INSIDE PAGE</title>
<jsp:include page="../../cssLoader.jsp"></jsp:include>
</head>
<body>
<h1>WEB-INF INSIDE PAGE</h1>
</body>
</html>
Note:
../../ depends upon folder level structure.
Related Topics
Generate Bank Account Number With Random
How to Convert a Kotlin Source File to a Java Source File
How to Check Type of Variable in Java
Why Is My Spring @Autowired Field Null
Java - How to Write My Arraylist to a File, and Read (Load) That File to the Original Arraylist
How to Convert Linkedhashmap to Custom Java Object
Regular Expression (Regex). How to Ignore or Exclude Everything in Between
Jparepository Findall() Returns Empty Result
Dynamically Change Application.Properties Values in Spring Boot
How to Download a Pdf File in Chrome Using Selenium Webdriver
How to Mock a Rest Template Exchange
How to Check If an Excel Cell Is Empty Using Apache Poi
How to Set a Default Entity Property Value With Hibernate
Java - Replace New Line Character by \N
How Would You Make a !!Say Command for a Discord Bot
How to Determine If a List of String Contains Null or Empty Elements
Calling Method That Exists in Child Classes But Not in Parent Class