Viriato
Viriato

Reputation: 3011

mvn tomcat:run does not pick up css files

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.

Upvotes: 3

Views: 5323

Answers (2)

Viriato
Viriato

Reputation: 3011

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"/>
    

Upvotes: 3

palacsint
palacsint

Reputation: 28885

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?

Upvotes: 3

Related Questions