mvn tomcat:run 不拾取 css 文件
我在此线程中提出了与此相关的另一个问题 在 Maven 项目中将 css 资源放在哪里
认为我没有将 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
我收到 Spring Framework 错误,提示 No mapping found for HTTP request with URI [ DispatcherServlet 中的 /appname/css/styles.css
在我使用传统项目样式(不是 maven)构建的其他 Spring Java 应用程序中,我能够使用上面的 url 从浏览器访问 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 文件。
I have asked another question related to this in this thread Where to put css resources in a maven project
thinking that I was not putting the css resources under the right location in a Maven project structure. Right now I have a folder under webapp called css and I have the css resources there. My problem is that when I do mvn clean tomcat:run
those css resources do not seem to be picked up and put appropriately in the target folder so my application cannot pick them up.
Does anybody know why?
UPDATE
My goal is to avoid building the war upon deployment for rapid development.I have changed my Maven command to: mvn clean war:exploded tomcat:run
The application runs fine but the css is not being picked up. If I go to the browser and type: http://localhost:8080/appname/css/styles.css
I get a Spring Framework error saying No mapping found for HTTP request with URI [/appname/css/styles.css
in DispatcherServlet
In other Spring Java apps that I have built using traditional project style (not maven) I was able to access the css from the borwser with the url above. I have made sure that the css folder is hanging straight out of the webapp folder.
Any ideas?
UPDATE: Adding 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>
I do not have the Dispatcher servlet mapped to /* and it still tries to access CSS files through the DispatcherServlet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tomcat:run
使用src
文件夹,而不是target
。您的css
文件位于src/main/webapp/css
中的合适位置。它们应该可以通过浏览器访问。mvn -X tomcat:run
打印配置。一些有趣的部分:您可以在此答案中找到更多详细信息: mvn tomcat7 :run - 它是如何工作的?
对于第二个问题/更新:我认为您将 Spring servlet 映射到
/*
并且它确实不处理静态内容。发布您的 web.xml 和 Spring 配置。也许你只需要一个mvc:resources
到您的 Spring 配置中。这也可能有用:如何处理静态Spring MVC 中的内容?tomcat:run
use thesrc
folder, not thetarget
. Yourcss
files are in a good place insrc/main/webapp/css
. They should be available from a browser.mvn -X tomcat:run
prints the configuration. Some interesting parts:You can find some more details in this answer: mvn tomcat7:run - How does it work?
For the second question/update: I think you mapped a Spring servlet to
/*
and it does not handle static content. Post yourweb.xml
and Spring configuration. Maybe you just need amvc:resources
into your Spring configuration. This also could be useful: How to handle static content in Spring MVC?只是为了澄清这是我必须做的:
确保在您的 servlet-context.xml 中具有如下内容:
如果 Web 应用程序下尚不存在名为 resources
将 css 文件夹与 css 文件一起放置在此处
参考我的 css 文件如下:
Just to clarify this is what I had to do:
Make sure that in your servlet-context.xml you have as follows:
Create a folder if does not already exist under webapps called resources
Place your css folder along with css files there
Reference my css file as follows: