0

I have a simple html page. But css files doesn't download in the page. I have an error 404 when I try show image in <style> tags in the page. Nothing works.

Catalog structure here.

home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <meta charset="utf-8">

    <title>Title</title>
    <link href="../../assets/css/style.css" rel="stylesheet">
    <link href="../../assets/css/bootstrap.css" rel="stylesheet">
    <style>
        /*h2{*/
            /*color: red;*/
        /*}*/
        body {
            background: url(../../assets/img/javarush.jpg);

        }
    </style>

</head>
<body>
<div class="home">
    <h2>Hello World!</h2>
</div>
<button class="bg-primary btn-primary btn-lg">Button</button>
</body>
</html>

style.css

.home {
    background-image: url('/assets/img/javarush.jpg');
    background-size: cover;

}
h2{
    color: #2b542c;
}

UPDATE: works only so

<style>
        <%@include file="/assets/css/style.css"%>
</style>

but image not found

UPDATE 2: in dispatcherServlet write <mvc:resources mapping="/assets/**" location="/assets/"/> and in .jsp write

<link href="<c:url value="/assets/css/bootstrap.css" />" rel="stylesheet">
    <link href="<c:url value="/assets/css/style.css" />" rel="stylesheet">

and it works great with image.

T Mac
  • 3
  • 5

2 Answers2

0

The way to add css file:
<link rel="stylesheet" type="text/css" href="mystyle.css">

Other info:

  • Starting with "/" returns to the root directory and continues from there
  • Starting with "../" moves one directory back and starts there
  • Starting with "../../" moves two directories back and starts there (and so on...)
  • To move forward, just start with the first subdirectory and keep moving forward
pol
  • 2,641
  • 10
  • 16
0
.home {
    background-image: url('../img/javarush.jpg');
}
Reknawn
  • 41
  • 3
  • Changed, but still doesn't work – T Mac Aug 29 '16 at 11:42
  • The path to get the css file seems correct since you need to jump out of pages and web-inf. If you put your css inline does it load the image ? – Reknawn Aug 29 '16 at 11:47
  • no. `body { background: url(../../assets/img/javarush.jpg); }` doesn't load – T Mac Aug 29 '16 at 11:52
  • Alright i've never used java to do web but it seems your file needs some context https://coderanch.com/t/628525/Servlets/java/CSS-stylesheet-work-JSP-files – Reknawn Aug 29 '16 at 12:00
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Aug 29 '16 at 14:18