JSP doesn't open CSS, Images and JS

99封情书 提交于 2020-01-10 04:35:11

问题


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=""/>


来源:https://stackoverflow.com/questions/35726851/jsp-doesnt-open-css-images-and-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!