外部CSS无法加载到网页中 [英] External CSS does not load in web page

查看:50
本文介绍了外部CSS无法加载到网页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring和Tomcat 7.0开发Web应用程序。当我在浏览器中测试页面时,它无法应用CSS,因为它无法加载外部样式表。

I am developing a web application using Spring and Tomcat 7.0. When I test the page in a browser it does not apply the CSS because it is unable to load the external stylesheet.

这是我的各种文件

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>

dispatcher-servlet.xml

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

welcomePage.jsp

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


推荐答案

您是说无法访问css网址(而不是不适用于您的jsp),对吗?浏览到/resources/dream.css时会有什么反应?

You're saying the css url is not accessible (as opposed to it not being applied to your jsp) right? What's the response when you browse to /resources/dream.css?

我以前遇到过问题,tomcat无法通过/ * servlet映射提供静态内容。我解决问题的方法是在web.xml中显式映射每种内容类型,包括.css文件,如下所示:

I have had issues before with tomcat not serving static content with a /* servlet mapping. The way I solved it was to explicitly map each content type in web.xml, including .css files, like so:

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

(其中默认 servlet是由Tomcat在conf / web.xml中作为org.apache预先定义的) .catalina.servlets.DefaultServlet)

(where the "default" servlet is predefined by tomcat in conf/web.xml as org.apache.catalina.servlets.DefaultServlet)

这篇关于外部CSS无法加载到网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆