Spring无法找到css / js文件

时间:2014-11-20 17:42:46

标签: java spring

我使用Maven,AJAX和JSON在Spring中创建了一个项目。我使用HTML文件而不是JSP,我在加载相关CSS,JS文件的HTML文件时遇到问题。

我已经看到了相关的问题,并且还阅读了几个关于这个问题的博客,但没有一个有效。 (mvc:资源是所有这些中的主要答案)。

Web.xml中

<?xml version="1.0" encoding="ISO-8859-1" ?>

<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>Paying Guest Management</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
</web-app>

应用context.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.payingguest" />
    <mvc:default-servlet-handler/>
    <mvc:resources location="/resources/" mapping="/resources/**" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/resources/" />
        <property name="suffix" value=".html" />
    </bean>


</beans>

对这些文件的引用:

<link rel="stylesheet" href="resources/css/bootstrap.min.css">
<link rel="stylesheet" href="resources/css/main.css">

我的应用程序的目录结构 Folder structure

请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:0)

请尝试以下。

<link href="<%=request.getContextPath()%>/resources/css/main.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

答案 1 :(得分:0)

这将为您提供应用程序上下文路径
 主机:localhost或IP地址
港口:8080
应用程序名称:myApplicattion
for:http://localhost:8080/myApplicattion

即使页面重定位到其他文件夹结构,

也能正常工作。

<link href="${pageContext.request.contextPath}/resources/css/main.css"
    rel="stylesheet" type="text/css">

<link href="${pageContext.request.contextPath}/resources/css/main.css"
    rel="stylesheet" type="text/css">