【发布时间】: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 这样的服务器。