-->

JSP doesn't open CSS, Images and JS

2019-01-27 04:54发布

问题:

I'm trying to put CSS, JS and IMGs on my JSP, but doesn't work, it's strange because I use c:url and "jstl/core"

I'm use JSP, jstl and servlet, no frameworks.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>clientes atuantes</title>
        <link rel="stylesheet" href="<c:url value="/bootstrap/css/bootstrap.min.css"/>">
        <link rel="stylesheet" href="<c:url value="/css/layout.css"/>">
        <script type="text/javascript" src="<c:url value="/bootstrap/js/jquery-1.12.0.js" />"></script>
        <script type="text/javascript" src="<c:url value="/bootstrap/js/bootstrap.min.js" />"></script>
    </head>
    <body>

        <main>
            <div class="col-md-12">
                <div class="panel panel-default espacamentoExterno5">

                    <div class="panel-body">
                        <p><img src="<c:url value="/img/ativo.gif"/>" /> ativo</p>
                        <p><img src="<c:url value="/img/congelado.gif"/>" /> congelado</p>
                        <p><img src="<c:url value="/img/cancelado.gif"/>" /> cancelado</p>

                    </div>
                </div>
            </div>
        </main>
    </body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:web="http://java.sun.com/xml/ns/javaee" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <display-name>PainelProduct</display-name>


</web-app>

回答1:

The url pattern "/" has a special meaning to the web server. It's used for default servlet mapping which is capable of serving the static content. The static content is JS, CSS, images, html resources. Directing these files to your servlet is wrong, because it's not capable to serve the static content.

Change the servlet mapping to something @WebServlet(urlPatterns="/incidente")

and use url localhost:8080/PainelProduct/incidente to forward to jsp page.

Check this question to learn more about servlets and servlet mapping Mapping a specific servlet to be the default servlet in Tomcat.



回答2:

You are not escaping the " when using inside the href value, use ' instead



回答3:

I have encountered the same problem and have not resolved it for a long time. Then I realized that it was a problem from the path of the images.

Wrong path is like that;

src>main>resources>images

The order of files(path) should definitely be as follows.

src>main>webapp>resources>images

and you should to call the function like that;

<img src="<c:url value="/resources/img/logo.png"/>" alt=""/>