【问题标题】:Link CSS and Images to Spring MVC maven Projcet将 CSS 和图像链接到 Spring MVC maven 项目
【发布时间】:2013-06-12 01:15:30
【问题描述】:

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

我想在这个项目中添加 css 图像。为此,我在 web-inf 目录中创建了一个资源文件夹,并在其中放置了一个图像。在我的调度程序 servlet xml 中,我添加了 3 行与 mvc 相关的内容。一行在 xmlns:mvc 中,另外 2 行是模式位置的最后 2 行:

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.

如果我从 bean 声明中删除 mvc 行并运行不带图像/css 的项目,则项目运行。

谁能帮我解决这个问题?如果上述方法不是在spring mvc项目中添加css和图片的方法,请指出正确的方法。

非常感谢!

【问题讨论】:

    标签: java eclipse spring maven spring-mvc


    【解决方案1】:
       webapp/resources/css 
       <head>
      <link href="<c:url value="/resources/css/commoncss.css" />" rel="stylesheet">
      </head>  
    

    如果不在 webapp 中查找资源文件夹,则在 webapp 中手动创建文件夹作为资源/css 文件夹,将您的 css 文件放入 css 文件夹中。

    【讨论】:

      【解决方案2】:

      你需要一点点改变。

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

      加上按照标准的maven项目新建一个文件夹,把你的资源放到这个里面

      src/main/resources
      

      【讨论】:

      • 嗨 Kalher 我在进行上述更改 cvc-complex-type.2.4.a 时收到以下错误:发现以元素“资源”开头的无效内容。 '{"http://www.springframework.org/schema/beans":import, "springframework.org/schema/beans":alias, "springframework.org/schema/beans":bean, WC[##other:"springframework.org schema/beans"]}' 之一是预计。
      • 确保你也声明了&lt;mvc:annotation-driven /&gt;元素。
      • 这个答案的 classpath 部分是无稽之谈。资源可能是标准的 Web 应用程序资源 (/src/main/webapp/**)。请删除它或以某种方式编写它,以免它暗示在类路径上拥有资源是必要的。
      • 这是一种解决方案,当然不需要将资源放在类路径中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2012-03-27
      • 2016-09-13
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多