Reputation: 122
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:
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();
Upvotes: 2
Views: 2348
Reputation: 2390
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 + "!/"));
}
Upvotes: 3
Reputation: 6079
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>
Upvotes: 0
Reputation: 49612
Maybe you are missing the context path in your url?
http://localhost:8080/<<context>>/css/main.css
Upvotes: 0