【问题标题】:404 When trying to link to a css file from jsp in Tomcat404 尝试从 Tomcat 中的 jsp 链接到 css 文件时
【发布时间】:2015-10-21 20:53:25
【问题描述】:

我正在尝试将我的 css 文件 (styles.css) 链接到我的 jsp 文件 (login.jsp),但是使用带有链接标签的 html,我无法链接 css!有什么建议吗?我确定我正确放置了路径,因为我创建了一个 css 文件(使用 jsp 表达式导入),并且我可以从 login.jsp 中读取它。似乎链接标签不适用于jsp。我提出了很多类似我的问题,但解决方案对我没有用。

这是我的项目的结构:

我的jsp文件头,

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Authenticator</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css">

</head>

谢谢, 罗德里戈

【问题讨论】:

  • 请分享您尝试链接css的jsp代码
  • 请将您的代码添加到您的问题中(即,您如何链接这两个文件?)
  • ../../css/styles.css
  • @AdamBuchananSmith 你确定吗?由于这是在服务器上运行的,我会推荐/css/styles.css
  • @msrd0 不,我不确定,这就是为什么它在 cmets 中而不是作为答案发布。

标签: css jsp tomcat


【解决方案1】:

一个tomcat服务器可以同时包含多个项目。然后,除了名为ROOT 的项目之外的所有项目都将作为localhost:8080/your-project 提供服务。因此,您可以使用以下链接指令来访问您的 css 文件:

<!-- for use from everywhere using an absolute path -->
<link rel="stylesheet" href="/your-project/css/styles.css">

<!-- for use from a specific directory (in this case
     from the project's root) using an relative path -->
<link rel="stylesheet" href="css/styles.css">

此外,您可能需要将 jsp 文件放在 WEB-INF 文件夹之外,有关详细信息,请参阅 the docs

【讨论】:

  • 我已经放了,但它不起作用,我的页面显示没有任何 css
  • @RodrigoG 所以调用localhost:8080/your-project/index.jsp 有效,但localhost:8080/your-project/css/styles.css 给你一个404?
  • 调用localhost:8080/Authenticator/login.jsp 不起作用,但另一个是的
  • 当它们位于 WEB-INF 文件夹中时,我无法直接从 url 路径访问任何 jsp 对吗?
  • @RodrigoG 我更新了我的答案(注意我在编辑之前拼错了 styles.css)
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 2021-08-09
  • 2014-01-13
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多