${pageContext.request.contextPath} is not working on plain HTML

僤鯓⒐⒋嵵緔 提交于 2019-12-08 11:19:06

问题


I'm using tomcat 7.0. Now I am facing an issue that could not load the css and js file. trying to add ${pageContext.request.contextPath} but does not work, also tried c:url tag, but getting syntax error in eclipse.

The structure of these files is:

WebContent
-content/css/lab3.css
html and js folder are under content folder as well

<?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>

Here is the html head:

<link rel="stylesheet" type= "text/css" href= "${pageContext.request.contextPath}/css/lab3.css"  media="screen, projection">

回答1:


EL expressions ${} doesn't run in a plain HTML file. It runs in JSP (and Facelets) files only. In your particular case, it'd be merely a matter of renaming lab3.html to lab3.jsp, or adding the following JSP servlet mapping to 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>

which will tell Tomcat to treat all .html files as if they are JSP files, with the consequence that EL will instantly work as well in .html files.

If none of the above is an acceptable solution, then you'd need to fall back to logical thinking and properly understanding and using relative paths. Like as in local disk file systems, the ../ brings you one folder up in URLs. Provided that the HTML file is in /content/html/lab3.html and the CSS file is in /content/css/lab3.css, then the following should do:

<link ... href="../css/lab3.css" />



回答2:


use ${request.contextPath} instead of ${pageContext.request.contextPath}



来源:https://stackoverflow.com/questions/18413906/pagecontext-request-contextpath-is-not-working-on-plain-html

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