2
votes

I'm having trouble getting my spring mvc webapp to work. I'm using Spring MVC with an embedded jetty server.

The problem is my mvc:resources tags don't work, and I have no idea why.

Here are the tags:

<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="js/"/>

My directory structure:

  • src
    • main
      • java
      • resources
        • META-INF
          • application-context.xml
          • web-context.xml
      • webapp
        • css
          • main.css
        • js
          • main.js

Now when I go to http://localhost:8080/css/main.css, I see this in the debug output:

Looking up handler method for path /css/main.css
Did not find handler method for [/css/main.css]
URI Template variables for request [/css/main.css] are {}
Mapping [/css/main.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@223c78ba] and 1 interceptor
Last-Modified value for [/css/main.css] is: -1
Trying relative path [main.css] against base location: ServletContext resource [/css/]
No matching resource found - returning 404

Why doesn't this work ? Is it my directory structure, or did I miss some configuration ?

I'd appreciate your help.

EDIT More information

I use Maven to build a fat jar with the shade plugin. I now added this in my pom.xml

<resources>
    <resource>
        <directory>src/main/webapp</directory>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

Now my final jar does contain the css directory, but still no luck.

This is my code to start the embedded jetty server

int port = config.getInt("server.port");

final Server server = new Server();
final ServerConnector serverConnector = new ServerConnector(server);
serverConnector.setPort(port);
server.setConnectors(new Connector[]{serverConnector});

final DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("classpath:META-INF/web-context.xml");

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(new ServletHolder("defaultServlet", servlet), "/*");

HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{context, new DefaultHandler()});
server.setHandler(handlers);

server.start();
server.join();
3
How are you building your project? With Maven? If so it might be helpful to include your pom.xml build info, or at least say whether you are building it as a jar or war. This is relevant because if you're building it as a jar using Maven, your src/main/webapp directory will not wind up being included in the jar by default. It might also be helpful to include your code where you are configuring your embedded Jetty server, as there is extra configuration involved in making resources available in an embedded Jetty webapp.sdouglass
I am building with Maven as a jar. In fact I'm using the shade plugin to build a fat jar. I'll edit my first post with more information.Vincent Rischmann
I have provided an answer below, which may be the missing config you need.sdouglass

3 Answers

3
votes

You may need some extra config for your servlet context, like this, to get the classpath stuff setup right for when you're running it via the jar you're building:

final Resource base = Resource.newClassPathResource(".");

if (base != null) {
    context.setBaseResource(base);
} else {    
    // running in a jar
    final URI uri = Service.class.getProtectionDomain().getCodeSource().getLocation().toURI();
    context.setBaseResource(Resource.newResource("jar:" + uri + "!/"));
}
0
votes

Maybe you are missing the context path in your url?

http://localhost:8080/<<context>>/css/main.css
0
votes

Make sure your application context has mvc schema xsd's mentioned.

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   

Basic application xml file with mvc, context schema should be like :

<beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

//your beans go here

</beans:beana>