我有一个 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>
请帮忙?
最佳答案
对我来说,这听起来不像是 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
现在,在我的 Spring 配置中,我将其更改为如下所示:
<mvc:resources mapping="/webstuff/**" location="/webstuff/" />
最后一部分是我如何在我的 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>
关于css - 为什么maven无法读取我的css和图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163517/