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
- META-INF
- webapp
- css
- main.css
- js
- main.js
- css
- main
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();