$ {pageContext.request.contextPath}不适用于纯HTML

J.L*_*J.L 0 tomcat el

我正在使用tomcat 7.0.现在我面临一个无法加载css和js文件的问题.尝试添加$ {pageContext.request.contextPath}但不起作用,也尝试了c:url标签,但在eclipse中遇到语法错误.

这些文件的结构是:

WebContent
-content/css/lab3.css
html和js文件夹也在内容文件夹下

<?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:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>lab3</display-name>
  <welcome-file-list>
    <welcome-file>/content/html/lab3.html</welcome-file>
  </welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)

这是html头:

<link rel="stylesheet" type= "text/css" href= "${pageContext.request.contextPath}/css/lab3.css"  media="screen, projection">
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

EL表达式${}不在纯HTML文件中运行.它仅在JSP(和Facelets)文件中运行.在您的特定情况下,这将会是重命名的仅仅是一个事lab3.htmllab3.jsp,或添加下面的JSP servlet映射到web.xml:

<servlet-mapping>
    <servlet-name>jsp</servlet-name> <!-- This is the default servlet name of Tomcat's own JSP servlet. To be sure, look in Tomcat's own web.xml for the exact name. -->
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

这将告诉Tomcat将所有.html文件视为JSP文件,结果EL将立即在.html文件中工作.

如果以上都不是可接受的解决方案,那么您需要回归逻辑思维并正确理解和使用相对路径.与本地磁盘文件系统一样,它../会在URL中为您提供一个文件夹.如果HTML文件在/content/html/lab3.html,并且CSS文件在/content/css/lab3.css,则以下内容应该:

<link ... href="../css/lab3.css" />
Run Code Online (Sandbox Code Playgroud)