无法在网络应用中加载CSS

时间:2017-03-18 00:21:55

标签: java css spring jsp servlets

嗨,我完全无法加载我的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>

1 个答案:

答案 0 :(得分:0)

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"/>