【问题标题】:Can't get CSS to load in web app无法让 CSS 在 Web 应用程序中加载
【发布时间】:2017-03-18 00:21:54
【问题描述】:

您好,我完全无法加载我的 CSS。我已经尝试了添加和删除斜杠的所有组合,使用 getContextPath(),并将 CSS 移动到我能想到的每个文件夹中。非常感谢任何帮助。谢谢。 Here's my structure

有问题的 JSP

<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>

<link rel = "stylesheet" type = "text/css" href = "/css/styles.css" />
</head>
<body class="background">
</body>
</html>

我的 web.xml

<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>To do List</display-name>

    <welcome-file-list>
        <welcome-file>login.do</welcome-file>
    </welcome-file-list>

     <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/todo-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

        <filter>
                <filter-name>springSecurityFilterChain</filter-name>
                <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
       </filter>

       <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
                <url-pattern>/*</url-pattern>
       </filter-mapping> 


</web-app>

我的控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
public class WelcomeController {

    @RequestMapping(value="/", method=RequestMethod.GET)    
    public String showLoginPage(ModelMap model){
        return "home";
    }

}

还有我的 servlet 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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="com.angels" />

        <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:resources mapping="/webjars/**" location="/webjars/"/>
        <mvc:annotation-driven />

    </beans>

【问题讨论】:

  • 刚开始我强烈建议你使用 SpringBoot。我知道互联网上有一些旧的教程(我自己从过时的 xml 配置类型教程中学习了 Spring),太容易出错了。否则,您将所有时间都花在配置上,这是一种浪费。
  • 谢谢,我会调查的。我一直在学习的教程提供了现成的配置文件,所以我还没有准备好 a) 重新创建或 b) 扩展功能。

标签: java css spring jsp servlets


【解决方案1】:

1.在您的spring config xml文件中,添加以下内容

<resources mapping="/resources/**" location="/resources/" /> 

2.将您的 CSS 文件放在 webapp/resources/css/styles.css

3.在你的 JSP 中引用 CSS

<link rel="stylesheet" href="%=request.getContextPath()%>/resources/css/styles.css"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2017-01-01
    • 2015-07-19
    • 1970-01-01
    • 2018-11-12
    • 2013-04-05
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多