我有一个使用 maven 创建的 Spring 项目。以下是我的目录结构。
我想向这个项目添加 css 图像。为此,我在 web-inf 目录中创建了一个资源文件夹,并在其中放置了一个图像。在我的调度程序 servlet xml 中,我添加了 3 行,与 mvc 有关。一行在 xmlns:mvc 中,另外两行是模式位置中的最后两行:
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
mvc xmlns 和 xsi 位置之前不存在,我添加了它们以便下面的行有效:
<mvc:resources location="/resources/**" mapping="/resources"/>
当我添加以上 3 行时,我的项目不起作用。我收到错误:
description The requested resource is not available.
如果我从 beans 声明中删除 mvc 行并运行没有图像/css 的项目,项目将运行。
谁能帮我解决这个问题?如果以上方法不是在spring mvc项目中添加css和images的方法,请指出正确的方法。
非常感谢!
最佳答案
你需要一个小的改变,即。
<mvc:resources mapping="/resources/**" location="/resources/" />
加上根据标准的maven项目创建一个新文件夹并将你的资源放在这个文件夹中
src/main/resources
关于java - 将 CSS 和图像链接到 Spring MVC maven 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124573/