【问题标题】:Cannot load local css in Spring MVC 4 (eclipse)无法在 Spring MVC 4 (eclipse) 中加载本地 css
【发布时间】:2015-06-06 07:11:05
【问题描述】:

我认为我的配置正确,但我无法加载 css。它似乎永远不会工作。我用谷歌搜索了大部分结果,但没有一个对我有用。我不确定我在哪里做错了。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.alias</groupId>
  <artifactId>YayaWine</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>YayaWine Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <spring.version>4.0.1.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring dependencies -->
    <dependency>
    <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
  </dependencies>

  <build>
    <finalName>YayaWine</finalName>
  </build>
</project>

web.xml

<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Archetype Created Web Application</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-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
s
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

调度程序-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <context:component-scan base-package="wine.controller" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <mvc:annotation-driven />
    <mvc:resources mapping="/css/**" location="/resources/css" />
    <mvc:default-servlet-handler />
</beans>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head lang="lang">
        <meta name="viewpoint" content="width=device-width, initial-scale=1">
        <link href="/css/bootstrap.css " rel="stylesheet" >
        <link href="/css/test.css " rel="stylesheet" >
        <title>hello</title>
    </head>
<body>
    <h2>Hello World!</h2>
</body>
</html>

【问题讨论】:

  • 您是否尝试使用浏览器的开发者工具并查看状态?
  • 你的项目结构中的css文件夹在哪里?
  • 我把我的css放在resources/css下

标签: css xml spring servlets model-view-controller


【解决方案1】:

更新jsp 中的link 标签,如下所示。

    <link href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet" >
    <link href="${pageContext.request.contextPath}/css/test.css" rel="stylesheet" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2019-11-13
    • 2018-04-21
    • 2023-03-11
    • 2019-01-03
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多