java - 将 CSS 和图像链接到 Spring MVC maven 项目

标签 java eclipse spring maven spring-mvc

我有一个使用 maven 创建的 Spring 项目。以下是我的目录结构。

enter image description here

我想向这个项目添加 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/

相关文章:

java - 传递给 Mapper 函数(setup、map、run、cleanup)的 Context 是否始终相同?

java - 无法在 spring boot 应用程序中 Autowiring 接口(interface)

java - 文件加载后程序退出

java - SWT 下拉菜单按钮视觉故障

c++ - Ubuntu 13.10 C++ OpenGL GLUT - 链接问题 - 未定义对 `glClearColor' 的引用

java - SWT Composite 在窗口最大化时不可见

eclipse - 如何更改 Eclipse 中的搜索 View ?

Spring Data Neo4j 4 - 找不到类的标识字段

Spring Security-Error 创建 bean 'org.springframework.security.filterChains'

java - JSTL 在 foreach 项目中使用 var