【问题标题】:How to display images and .css file in .jsp如何在 .jsp 中显示图像和 .css 文件
【发布时间】:2013-10-29 05:20:31
【问题描述】:

我有一个文件 FixHome.jsp,它试图显示来自 style.css 的徽标和格式。但是,当我运行 .jsp 文件时,图像不会出现,并且 stye.css 文件的格式也没有实现。

FixConnections 项目下的文件位置如下:

/WebContent/FixHome.jsp

/WebContent/Resources/style.css

/WebContent/Resources/needhamlogo2.bmp

我正在使用 Eclipse 并运行 Tomcat 7.0

<html>
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/Resources/style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Fix Connections</title>
</head>
<body>
<table bgcolor="black" id="shell" height="100%" width="100%">
 <tr height="30">
  <td bgcolor="white">
     <img src="${pageContext.request.contextPath}/Resources/NeedhamLogo2.bmp"/> 
  </td>
  <td bgcolor="white"> 
    FIX Connections 
  </td> 
</tr> 
</body>
</html>

如果我正确引用了图像和 .css 文件,请告诉我。

【问题讨论】:

    标签: jsp contextpath


    【解决方案1】:

    只使用相对的href:

    href="Resources/style.css"
    

    或者,添加一个前导斜杠。 contextPath 只返回一个字符串,因此您的 href 呈现如下内容:

    href="ctxt/Resources/style.css"
    

    当您需要以下之一时:

    href="/ctxt/Resources/style.css"
    href="Resources/style.css"
    

    【讨论】:

    • 我试过href="Resources/style.css" 但它仍然没有在页面上生效。会不会有别的问题?
    • 你不能检查在你的 html 中呈现了什么,只看 html 源代码。之后检查是否可以访问呈现的 URL。
    • 现在可以使用了。似乎我必须重新启动 eclipse 才能生效。谢谢。
    【解决方案2】:

    尝试使用默认 servlet 查看文档 http://tomcat.apache.org/tomcat-6.0-doc/default-servlet.html

    在jsp中(示例)

        <link href="css/custom.css" rel="stylesheet" type="text/css" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    

    在 web.xml(示例)中

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2012-06-28
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2019-02-11
      • 2017-06-28
      相关资源
      最近更新 更多