正在学习 JSP,遇到无法加载 css/js 内容的问题。我有一个 jsp 页面,其中我使用标准 html 链接和脚本标记引用了 bootstrap css 和 js:
<link rel="stylesheet" type="text/css" link="/bootstrap/css/bootstrap.css" />
和
<script src="/WebIntro/bootstrap/js/bootstrap.js"></script>
它们都不起作用,Chrome 在控制台上为我提供了以下信息:
Resource interpreted as Stylesheet but transferred with MIME type text/plain:
如果我使用 include 指令,它适用于 css,但会将所有内容拉入文件:
<%@include file="/bootstrap/css/bootstrap.css"%>
jsp 具有以下@page 和元标记:
<%@page contentType="text/html" %>
和
<meta http-equiv = "Content-Language" content = "en"/>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
我尝试使用谷歌搜索并找到了 web.xml 的 mime-mapping 元素,但以下似乎没有影响:
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
如果需要进行一些标准的 Tomcat 配置,我不知道,因为我是 Tomcat 的新手;使用 Tomcat 7 和 eclipse,这是在一个 maven 元素中。
最佳答案
我的解决方案很奇怪。自从我在 eclipse/maven 中构建元素以来,对 web.xml 文件的更改并未反射(reflect)在 tomcat webapp 应用程序目录中的 web.xml 中。包中的其他所有内容都可以正常更新(据我所知),除了 web.xml?
所以我在 eclipse 中的 target >> project-SNAPSHOT >> WEB-INF >> web.xml 中找到了 web.xml 文件(应该是派生的)。在那里进行强制更改并且它起作用了,更改被拾取并且我原来的问题消失了。
我猜我无意中破坏了某种类型的依赖关系链接或某种方式,但一旦我重新添加了 servlet、映射等,一切正常。
奇怪。
关于java - JSP Tomcat7 静态 css 和 js 文件未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278398/