我有一个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>
请帮忙吗?
答案 0 :(得分: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
现在,在我的Spring配置中,我将其更改为:
<mvc:resources mapping="/webstuff/**" location="/webstuff/" />
最后一部分是我如何参考css&amp;我的jsp中的图片:
<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>
答案 1 :(得分:0)