mvn tomcat:run 不拾取 css 文件

发布于 2024-12-11 07:39:19 字数 2516 浏览 0 评论 0原文

我在此线程中提出了与此相关的另一个问题 在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2024-12-18 07:39:19

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 - 它是如何工作的?

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

tomcat:run use the src folder, not the target. Your css files are in a good place in src/main/webapp/css. They should be available from a browser.

mvn -X tomcat:run prints the configuration. Some interesting parts:

[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

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 your web.xml and Spring configuration. Maybe you just need a mvc:resources into your Spring configuration. This also could be useful: How to handle static content in Spring MVC?

温柔女人霸气范 2024-12-18 07:39:19

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

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

    <资源映射=“/资源/**”位置=“/资源/”/> 
    
  2. 如果 Web 应用程序下尚不存在名为 resources

  3. css 文件夹与 css 文件一起放置在此处

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

    
    

Just to clarify this is what I had to do:

  1. Make sure that in your servlet-context.xml you have as follows:

    <resources mapping="/resources/**" location="/resources/" /> 
    
  2. Create a folder if does not already exist under webapps called resources

  3. Place your css folder along with css files there

  4. Reference my css file as follows:

    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/960.css"/>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文