My file structure is as follows;
webapp
resources
foo.css
WEB-INF
home.jsp
mgr
ha.jsp
File home.jsp has the following:
<link rel="stylesheet" href="resources/foo.css
File ha.jsp has the following:
<link rel="stylesheet" href="../resources/foo.css
In Spring I have the following statement in my extension of WebMvcConfigurerAdapter:
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
Spring is happy and both JSP files render properly. But both JSP files cause errors with the Eclipse JSP editor because the editor cannot resolve the file locations. To fix the errors with the JSP editor I would have to code file home.jsp like
<link rel="stylesheet" href="../../resources/foo.css
And code ha.jsp like
<link rel="stylesheet" href="../../resources/foo.css
But this causes the web page to fail because Spring cannot resolve the files. So I tried coding in Spring the following:
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/","../../resources/");
This doesn't fix Spring but at least the JSP editor is happy. So how would I get out of the conflict and make both Spring and the JSP editor happy?
I read many possible explanations with this one being the best. Spring MVC mvc:resources location attribute
But I still could not get the problem resolved. Any help would be appreciated.
Related
I want to connect my .html file (containing Bootstrap's elements) with Bootstrap's .css file. I saw a lot of variants like:
<link href="resources/static/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap.min.css" rel="stylesheet">
...but none of them was correct. In my case, I mean.
How it looks when I include in my code some of these line
How it looks when I include following line instead of these above:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
The conclusion are such that the app can see .css from the Internet but there is some problem with local file.
Here I sent .html file code:
http://wklej.org/id/3272762
If someone want to check folder structure, there is screenshot of my project in IntelliJ:
http://s6.ifotos.pl/img/idescreen_qrwrweq.jpg
I read on Stack there could be some problem with configuration but their solutions didn't seem appropriate considering fact that I use Spring Boot, not pure Spring without auto-config. Maybe there should be something added in application.properties? I saw also tips to put all .htmls with .css in one folder but... well, I don't think it is a good way.
Does someone know what I do wrong and what can I do to correct it to work properly?
EDIT.
If you are looking for solution, please read comments directly under first post
Try this above the other links:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
Hope this works
As I see in file structure templates too is under the resources so the path should be href="../../static/css/bootstrap.min.css"
After receiving inputs of the Source HTML File and Bootstrap CSS file.
From the HTML file, ../ this will make it go one folder back which is resources, then go two folders forward static/css/ then there the bootstrap.min.css will be found!
HTML:
Propagander(main folder)/src/main/resources/templates
CSS:
Propagander/src/main/resources/static/css
The correct link should be.
<link rel="stylesheet" href="../static/css/bootstrap.min.css">
Please let me know if this fixes your issue!
I saw an error in file path you added here:
<link href="resources/static/css/bootstrap.min.css" rel="stylesheet" media="screen">
instead write:
href="../../resources/static/css/bootstrap.min.css"
EDIT:
href="../../static/css/bootstrap.min.css"
I'm building a dynamic web project using JSP and Servlets with Tomcat 7.0 in Eclipse. Everything works fine when I have the CSS-code inside the JSP-file, but when I'm moving it into a seperate file, I can't get the JSP-file to find the CSS-file. My file strucuture look like this:
MyProject
-src
-build
-lib
-WebContent
--MyJsp.jsp
--MyCss.css
--META-INF
--WEB-INF
This is what I've tried:
<link rel = "stylesheet" type ="text/css" href = "/MyCss.css"/>
<link rel = "stylesheet" type ="text/css" href = "MyCss.css"/>
<link rel = "stylesheet" type ="text/css" href = "/MyProject/WebContent/MyCss.css"/>
<link rel = "stylesheet" type ="text/css" href = "${pageContext.request.contextPath}/MyCss.css"/>
Literally nothing works. The JSP-pages doesn't find the CSS-file. (I'm not using a web.xml and the first that is loaded is a servlet, if that has someting to do with it).
I know that other threads about this issue exists, but none of the answers works for me.
Hank
Create a new folder under WebContent as css and add your css file
and then try the below code in jsp.
<link rel="stylesheet" href="css/yourfile.css" type="text/css" />
You can add firebug plugin in firefox and try to load the jsp file, which will help you in debugging,like whether the css file is loaded.Hope this helps.
There may be a lot of ideas floating around, but I'd suggest bisecting the problem until a solution arrives.
Open the page with the external CSS. Look at the page source (from your browser or just save the resulting HTML somewhere). How does the link look? (Hint: It should be something like <link rel="stylesheet" type="text/css" href="…"/>-
Open the CSS in the browser. Does the CSS load? If so, you've most likely made an error in the HTML-generating part of your HTML, but I think it is unlikely.
Look into your WAR file (perhaps with jar tf or rename it to .zip and look with some unzipper) – is the CSS included? Maybe it was discarded in your build process? Some builds reserve a /resources/ folder for stuff like extra CSS files.
If the file is there, look at the tomcat logs, perhaps you will find an error there.
Your css resource link may be wrong and you may need to provide a relative link from your JSP file. Try to link it as follows (note the use of the two periods to mention a one dir above your JSP file):
<link href="../MyCss.css" rel="stylesheet" type="text/css">
And as #dev already mentioned in his comment, ${pageContext.request.contextPath}/MyCss.css as a path should already do the trick.
The problem is that the file is not being served as a resource. You need to tell the server to expose a folder of resources to a certain path and move that file there. Since I'm not familiar with what you are using to setup your application (if you're using spring or something else), I can't give anymore insight on the problem.
I am facing a problem in jsp-servlet, when I am setting up my index.jsp as welcome-file (default page to run first as soon as the project gets run) at that time the CSS and JS files are not rendered.
My project Hierarchy is like this
-ProjectName
-User
-css
-js
-index.jsp
And also when I run my project the URL in the browser will be localhost:8080/ProjectName so here index.jsp is called but without CSS and JS files.
Edit: Finally, I had solved this issue by copying the CSS and JS to the root directory also. i.e. I have CSS and JS # "User" as well as "ProjectName".
Try with the context path,
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css">
will get you the path of the css file in the css folder. or with the scriptlet,
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/style.css">
However, it is not recommended to you use the scriptlets in the jsp.
I have a problem with dynamically loaded CSS files in a JSF application running on tomcat 7.
If I load the CSS file on a static way like
<link href="mystyle.css" rel="stylesheet" type="text/css">
all is ok and the web page looks fine. But if I trying to load the CSS file dynamically like
<link href="#{pageManagerBean.getCSSFile()}" rel="stylesheet" type="text/css">
where pageManagerBean.getCSSFile() returns "mystyle.css", the CSS file will be not loaded. The browser gets from tomcat a "404 - Not found". But if I doing a browser refresh the CSS file is found and the web site looks fine.
I created a jsp page as follows:
<%# page contentType="text/css" %>
<html>
<head>
<title>Login page</title>
<link href="/css/loginstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1> India welfare</h1>
<p> welcome </p>
</body>
</html>
and named it as login.jsp
and i also created a css file called loginstyle.css and the code of the .css file is as follows:
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
the directory structure for css and jsp's are as follows:
webcontent/welfare_web/css for .css files and
webcontent/welfare_web/login for jsp files
the programming editor is eclipse and the server i am using is tomcat 7.0. when i am trying to run the login.jsp file using tomcat server. The css file is not showing any effect. i mean the output is normal text and is not as per the CSS file.
please help me how to make the .css file to effect the jsp file.
css href link is incorrect. Use relative path instead:
<link href="../css/loginstyle.css" rel="stylesheet" type="text/css">
You can write like that. This is for whenever you change context path you don't need to modify your jsp file.
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />
I use this version
<style><%#include file="/WEB-INF/css/style.css"%></style>
I had the same problem too. Then i realized that in the MainPageServlet the urlPatterns parameter in #WebServlet annotation contained "/", because i wanted to forward to the MainPage if the user entered the section www.site.com/ . When i tried to open the css file from the browser, the url was www.site.com/css/desktop.css, but the page content was THE PAGE MainPage.jsp. So, i removed the "/" urlPattern and now i can use CSS files in my jsp file using one of the most common solutions (${pageContext.request.contextPath}/css/desktop.css).
Make sure your servlet doesn't contain the "/" urlPattern.
I hope this worked for u too,
- Axel Montini
if everything seems correct, and despite it still does not work I invite you to load the statics files in the web.xml like this
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/includes/*</url-pattern>
</servlet-mapping>
after
<!-- bootstrap css -->
<link rel="stylesheet" type="text/css" href="/includes/asserts/css/bootstrap.min.css"/>
use this it worked for me
<style><%#include file="/WEB-INF/view/style/style.css"%></style>
For CSS :
<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
For JS :
<script type="text/javascript" src="js/bootstrap.js"></script>
Files location:
└───src
└───main
├───java
└───webapp
├───css/bootstrap.css
├───js/bootstrap.js
├───META-INF
└───WEB-INF/index.jsp
└───lib