【问题标题】:JSP can't find stylesheetJSP 找不到样式表
【发布时间】:2011-10-01 09:15:22
【问题描述】:

层次结构:
WEB-INF/jsp
WEB-INF/样式

我在我的 JSP 文件中链接样式表,该文件位于 WEB-INF/jsp:

 <link rel="stylesheet" type="text/css" href="../styles/reset.css" />

但它不起作用! 当我打开我的应用程序时,没有样式,并且由 Tomcat 编写:

<html><head><title>Apache Tomcat/7.0.14 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The requested resource () is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.14</h3></body></html>

我该如何克服它?

我的项目: http://img835.imageshack.us/img835/6159/73536381.jpg

我正在使用 Spring 框架 3。 我已将包含样式和图像的文件夹放在 WEB-INF 之外,但它仍然无法正常工作。 我的jsp文件是这样写的:

<link rel="stylesheet" type="text/css" href="resources/styles/styles.css" />

而且它不起作用......

【问题讨论】:

  • 发布您的文件夹结构和您尝试用于访问静态文件的 URL。
  • 您需要格式化您的 HTML。

标签: java jsp coding-style jstl stylesheet


【解决方案1】:

我找到了http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources 所以现在我的 rus-servlet.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        <context:component-scan base-package="rus.web"/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        <mvc:annotation-driven/>
        <mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>

但是这在 JSP 中还是不行!

<link rel="stylesheet" type="text/css" href="resources/styles/styles.css" />

我的 spring 框架有 3.0.5 版本。

【讨论】:

  • 好的,我解决了这个问题!我必须在jsp文件中写:
【解决方案2】:

存储在 WEB-INF 中的所有文件都故意对外不可见。因此,指向 WEB-INF/something 的 URL 将始终导致 404。WEB-INF 用于存储应用程序所需的资源,但必须保持对外部不可见。

将 CSS 文件放在 WEB-INF 之外。

【讨论】:

  • 嗯,我已经将我的 "styles" 文件夹和我的所有样式表一起移到了 WEB-INF 之外,但它仍然无法正常工作......
【解决方案3】:

默认情况下,Tomcat 不会提供来自 WEB-INF/ 的静态文件,请将您的 css 上移一级,使 css 为“./styles/some.css”。由于您在 WEB-INF/jsp 中有您的 JSP,我假设您使用的是 JSF 或其他一些外观框架?

【讨论】:

  • 我正在使用spring框架。我用 css 将我的文件夹向上移动了一个杠杆,但它仍然不起作用......
猜你喜欢
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 2017-04-01
相关资源
最近更新 更多