Pregunta

My project structure:

folders

hello.jsp:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
    <meta charset="utf-8">
    <link href="<c:url value="/resources/myStyle.css" />" rel="stylesheet">
</head>
<body>
<h1>Text</h1>
<p>Text.</p>
</body>
</html>

myStyle.css:

body {
    font-family: Arial, Verdana,  sans-serif;
    font-size: 11pt;
    background-color: #f0f0f0;
    color: #333;
}
h1 {
    color: #a52a2a;
    font-size: 24pt;
    font-family: Georgia, Times, serif;
    font-weight: normal;
}
p {
    text-align: justify;
    margin-left: 60px;
    margin-right: 10px;
    border-left: 1px solid #999;
    border-bottom: 1px solid #999;
    padding-left: 10px;
    padding-bottom: 10px;
}

Could someone please explain me why css doesn't work? I use Intellij IDEA as IDE and Tomcat.

This works though:

<style>
        <%@include file="/resources/myStyle.css" %>
</style>
¿Fue útil?

Solución

Simply try with any one. All these are relative path with respect to webapp folder.

<link rel="stylesheet" type="text/css" href="/resources/myStyle.css" >

OR

<link rel="stylesheet" type="text/css" 
                href="<%=application.getContextPath() %>/resources/myStyle.css" >

OR

<jsp:directive.include file="/resources/myStyle.css" />

JSP - The include Directive

The filename in the include directive is actually a relative URL. If you just specify a filename with no associated path, the JSP compiler assumes that the file is in the same directory as your JSP.

The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top