Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Jetty static content redirection (for use with angularJs html5 mode)

Hi there,

I'm doing an AngularJs Single Page App which I want to have work with html5 mode on and served up by Jetty embedded (For context see this question:http://stackoverflow.com/questions/17646843/angularjs-html5-mode-how-do-direct-links-work-without-server-specific-changes). I would like to have static content served up directly from "/" (which works by simply leaving my resources at the top level of the webapp directory). I also have a REST api served up by jersey at /api/*.

My main method kind of looks like this:

        String docRoot = "src/main/webapp"

        final Server server = JettyHttpContainerFactory.createServer(baseUri, false);

        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/");
        webAppContext.setDescriptor(docRoot + "/WEB-INF/web.xml");
        webAppContext.setResourceBase(docRoot);
        webAppContext.setParentLoaderPriority(true);

        server.setHandler(webAppContext);
        server.start();
        server.join();

So in my mind what I would like to see happen is:
- If anything occurs below /api, it should always be handled by Jersey. So errors or 400's can still be returned in json by jersey rather than by the jetty container.
- If anything is requested out of /, excluding /api then jetty should just serve it up. If a path does not exist (in detail I say 'path' meaning anything that does not have an extension) then I want to always return /index.html. If an asset doesn't exist (e.g. /images/kitten.jpg) then it shouldn't redirect and just return a 400.


This is the closest I've seen to a good solution but it would mean putting all static resources under WEB-INF which doesn't really make sense: http://stackoverflow.com/questions/18836455/serve-static-pages-rest-services-with-spring

I've also looked at:http://wiki.eclipse.org/Jetty/Feature/Rewrite_Handler which seems right but has the same issue as the above link.

Any help would be awesome!

::mark

Back to the top