2011-11-14 41 views
2

SpringとTomcat 7.0を使用してWebアプリケーションを開発しています。ブラウザでページをテストすると、外部スタイルシートを読み込めないためCSSは適用されません。ここで 外部CSSがWebページに読み込まれない

は私のさまざまなファイル

web.xmlの

<?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_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>MyProject</display-name> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet> 
    <servlet-name>ResourceServlet</servlet-name> 
    <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 


    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping>  
    <servlet-mapping> 
    <servlet-name>ResourceServlet</servlet-name> 
    <url-pattern>/resources/*</url-pattern> 
</servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

ディスパッチャ-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

    <bean id="userService" class="com.abdus.service.UserServiceImpl" /> 

    <context:component-scan base-package="com.abdus.web" /> 
    <bean 
     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
    <bean 
     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

</beans> 

であり、これは、JSPページからのヘッドである

welcomePage.jsp

<meta charset="utf-8"/> 
    <title>Welcome</title> 
    <link rel="stylesheet" href="/resources/dream.css" type="text/css" /> 
+0

あなたがのための完全なクラス名についてよろしいですResourceServlet。私はそれがorg.springframework.web.servlet.ResourceServlet(Spring 3.xとSpring 2.5)であると信じています。 – DwB

+0

私がorg.springframework.web.servlet.ResourceServletを使用すると、次の例外が発生します –

答えて

5

あなたはされていない、それとは対照的に、(CSSのURLにアクセスできないと言っていますあなたのJSPに適用されます) /resources/dream.cssにアクセスしたときの応答は何ですか?

以前は、静的コンテンツに/ *サーブレットマッピングを提供していないTomcatに関する問題がありました。 「デフォルト」のサーブレットは次のようにのconf/web.xmlにTomcatが事前に定義されて

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

(:私はそれが明示的にそうように、.cssファイルを含むweb.xmlファイル内の各コンテンツタイプを、マッピングすることでした解決方法org.apache.catalina.servlets.DefaultServlet)

0

あなたはJSTLタグを使用することができます。

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<link href="<c:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" /> 

それとも、完全なリソースのパスを使用することができます。

<base href="http://localhost:8080/myapp/" /> 
<link href="resources/style.css" rel="stylesheet" type="text/css" /> 
1

実際にはパスの問題でした。これは私のディレクトリ構造でした

MyProject/WEB-INF/resources/dream.css jspリンクで私はhref = "/ resources/dream.css"として与えていましたが、href = "MyProject/resources /dream.css "

しかし、私はそれを解決しwasyは私が MyProjectと/リソース/ dream.cssと のhref =「MyProjectと/リソース/ dream.css」などのディレクトリ構造を作っている

関連する問題