为什么maven无法读取我的CSS和图像

use*_*665 1 css image maven

我有一个maven项目,我想在项目上使用css,但它只显示index.jsp页面,没有任何图像和CSS.它能取决于什么?

我把我的CSS和图像放在资源下面,这是我的servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xsi:schemaLocation="
        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
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mvc:annotation-driven />

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

    <mvc:default-servlet-handler />

    <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:import resource="controllers.xml" />

</beans:beans>
Run Code Online (Sandbox Code Playgroud)

请帮忙吗?

Sha*_*ood 5

这对我来说听起来不像Maven问题.我想你可能把Spring示例资源映射与标准的Maven资源文件夹混淆了.在这种情况下,您的图像应该相对于src/main/webapp文件夹(即src/main/webapp/resources不是src/main/resources).Maven本身对src/main/webapp/resources文件夹没有任何特殊处理.

更新

需要明确的是,webapp下的文件夹"resources"可以命名为任何名称.为了减少任何混淆,我将我的名字改名为"webstuff".举个例子,我有

src/main/webapp/webstuff
src/main/webapp/webstuff/css/base.css
src/main/webapp/webstuff/images/globe.png
Run Code Online (Sandbox Code Playgroud)

现在,在我的Spring配置中,我将其更改为如下所示:

<mvc:resources mapping="/webstuff/**" location="/webstuff/" />
Run Code Online (Sandbox Code Playgroud)

最后一部分是我如何在我的jsp中引用css和图像:

<html>
    <link rel="stylesheet" href="webstuff/css/default.css" type="text/css">
    <body>
    <h1>Hello World!</h1>
    <img src="webstuff/images/globe.png">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)