【问题标题】:mvn tomcat:run does not pick up css filesmvn tomcat:run 不拾取 css 文件
【发布时间】:2011-12-11 22:22:40
【问题描述】:

我在这个帖子Where to put css resources in a maven project中问了另一个与此相关的问题

认为我没有将 css 资源放在 Maven 项目结构中的正确位置。现在我在 webapp 下有一个名为 css 的文件夹,并且那里有 css 资源。我的问题是,当我执行 mvn clean tomcat:run 时,这些 css 资源似乎没有被拾取并适当地放在目标文件夹中,因此我的应用程序无法拾取它们。

有人知道为什么吗?

更新

我的目标是避免在部署时为快速开发而战。我已将 Maven 命令更改为:mvn clean war:exploded tomcat:run

应用程序运行良好,但 css 没有被拾取。如果我在浏览器中输入:http://localhost:8080/appname/css/styles.css

我在 DispatcherServlet 中收到一个 Spring Framework 错误提示 No mapping found for HTTP request with URI [/appname/css/styles.css

在我使用传统项目样式(不是 maven)构建的其他 Spring Java 应用程序中,我能够使用上面的 url 从 borwser 访问 css。我已确保 css 文件夹直接挂在 webapp 文件夹之外。

有什么想法吗?

更新:添加 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml
                 classpath*:META-INF/spring/applicationContext*.xml                  
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我没有将 Dispatcher servlet 映射到 /*,它仍然尝试通过 DispatcherServlet 访问 CSS 文件。

【问题讨论】:

  • 这不是您问题的答案,所以我不会将其作为一个问题发布,但您可能需要考虑将静态文件托管在 外部 Tomcat 中,通过网络像 Apache 或 nginx 这样的服务器。

标签: css spring tomcat maven-2


【解决方案1】:

tomcat:run 使用src 文件夹,而不是target。您的css 文件位于src/main/webapp/css 的好位置。它们应该可以从浏览器中获得。

mvn -X tomcat:run 打印配置。一些有趣的部分:

[INFO] Preparing tomcat:run
[DEBUG] (s) resources = [Resource {targetPath: null, filtering: false, 
    FileSet {directory: /workspace/webtest1/src/main/resources, 
    PatternSet [includes: {}, excludes: {}]}}]
...
[DEBUG] (f) warSourceDirectory = /workspace/webtest1/src/main/webapp

您可以在此答案中找到更多详细信息:mvn tomcat7:run - How does it work?

对于第二个问题/更新:我认为您将 Spring servlet 映射到 /* 并且它不处理静态内容。发布您的 web.xml 和 Spring 配置。也许你只需要一个mvc:resources 到你的 Spring 配置中。这也可能有用:How to handle static content in Spring MVC?

【讨论】:

  • 用 web.xml 更新了问题。那么,当您将 css 放在正确的位置 src/main/webapp/css 下时,您是否必须使用
【解决方案2】:

只是为了澄清这是我必须做的:

  1. 确保在您的 servlet-context.xml 中有如下内容:

    <resources mapping="/resources/**" location="/resources/" /> 
    
  2. 如果在名为 resources

  3. 的 webapps 下尚不存在文件夹,请创建一个文件夹
  4. 将您的 css 文件夹与 css 文件一起放在那里

  5. 参考我的css文件如下:

    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/960.css"/>
    

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 2011-06-11
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2018-01-26
    • 2013-07-10
    • 2023-01-21
    • 1970-01-01
    相关资源
    最近更新 更多