【问题标题】:"Resource interpreted as Stylesheet but transferred with MIME type text/html" issue java web project“资源解释为样式表,但使用 MIME 类型文本/html 传输”问题 java web 项目
【发布时间】:2015-02-24 23:49:19
【问题描述】:

我的动态 Web 项目有问题。 树状是

网页内容
|
|
引导
CSS
js
WEB-INF
|
|
连接.jsp

然后在 connection.jsp 我做这个:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-theme.css"/>
<link rel="stylesheet" type="text/css" href="css/connection.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/register.js">     </script>
<title>Jweb</title>
</head>
<body>

所有 css 都不适用,在 chrome 上的控制台中我有这个:

Resource interpreted as Stylesheet but transferred with MIME type text/html: 
"http://localhost:8080/jweb/bootstrap/css/bootstrap-theme.css". localhost/:10
Resource interpreted as Stylesheet but transferred with MIME type text/html:          "http://localhost:8080/jweb/bootstrap/css/bootstrap.css". localhost/:9
Resource interpreted as Stylesheet but transferred with MIME type text/html:    "http://localhost:8080/jweb/css/footer.css". localhost/:11
Resource interpreted as Stylesheet but transferred with MIME type text/html:  "http://localhost:8080/jweb/css/header.css". localhost/:12
Resource interpreted as Script but transferred with MIME type text/html:     "http://localhost:8080/jweb/js/jquery.js". localhost/:14
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8080/jweb/js/bootstrap.js". localhost/:15
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/jweb/css/connection.css". 

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<jsp-config>
<jsp-property-group>
 <url-pattern>*.jsp</url-pattern>
 <include-prelude>/WEB-INF/taglibs.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
</web-app>

我在 2 天前尝试解决此问题,但我仍在解决此问题。 你能帮我解决吗?

【问题讨论】:

  • 好的,但我不知道我必须在哪里设置这个?我尝试添加 并将内容类型设置为“text/css”但不工作。我试图将内容类型属性设置到我的 servlet 中。和其他一些解决方法(在互联网上找到的所有解决方案)但没有人工作
  • 请参阅您的 Web 服务器文档。这取决于服务器软件。
  • 告诉我们您的服务器类型(tomcat、glassfish、jboss)并发布您的 web.xml
  • @JérômeCampeaux 我的意思是把它贴在你的问题上......而不是作为答案。
  • 对不起,我把它放在我的问题上

标签: java css jakarta-ee include mime


【解决方案1】:

快速“mime mapping web.xml”搜索建议您在web.xml 中使用mime-mapping 元素:

<mime-mapping>
    <extension>css</extension>
    <mime-type>text/css</mime-type>
</mime-mapping>

例如(在结束 &lt;/web-app&gt; 行之前的新行):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<jsp-config>
<jsp-property-group>
 <url-pattern>*.jsp</url-pattern>
 <include-prelude>/WEB-INF/taglibs.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<mime-mapping>
    <extension>css</extension>
    <mime-type>text/css</mime-type>
</mime-mapping>
</web-app>

【讨论】:

  • 感谢回复。我做到了,但它仍然无法正常工作。我只是为了做这个手术吗?
  • @JérômeCampeaux:据我所知,这就是所有应该要求的。您必须确保重新启动服务器等。
  • 是的,我确定,我重新启动了服务器,在 chrome 的开发人员视图中我可以看到 MIME 的相同问题
【解决方案2】:

这是一个示例 web.xml 供您尝试使用。

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

                <servlet>
                    <servlet-name>welcome</servlet-name>
                    <jsp-file>index.jsp</jsp-file>
                    <load-on-startup>1</load-on-startup>
                </servlet>

                <servlet-mapping>
                    <servlet-name>welcome</servlet-name>
                    <url-pattern>/*</url-pattern>
                </servlet-mapping>

                <jsp-config>
                    <jsp-property-group>
                        <url-pattern>*.jsp</url-pattern>
                        <include-prelude>/WEB-INF/taglibs.jsp</include-prelude>
                    </jsp-property-group>
                </jsp-config>

</web-app>

【讨论】:

  • 我没有使用 servlet 映射,因为我在我的 servlet 上使用了 @WebServlet("/")。如果我这样做,我会因为描述之间的冲突而出错
猜你喜欢
  • 2018-08-16
  • 2012-09-20
  • 2018-11-09
  • 2012-05-20
  • 2014-11-19
  • 2013-05-04
  • 2011-12-13
  • 2017-10-12
  • 2016-03-19
相关资源
最近更新 更多