0

In a JSF application, we have the directory hierarchy:

webapp
  xhtml
    login.xhtml
    main.xhtml
    search.xhtml
  css
    main.css
    extra.css
  js
    jquery.js

etc. The servlet mapping is:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

This works fine, but the URLs of our web app look like this:

http://localhost/myapp/xhtml/login.xhtml
http://localhost/myapp/xhtml/search.xhtml

We would like to have simpler URLs by dropping the /xhtml part, i.e. http://localhost/myapp/login.xhtml

I could not find any way to accomplish this. Is there some way to do this in the <servlet-mapping>? Do I need some additional framework?

sleske
  • 81,358
  • 34
  • 189
  • 227

1 Answers1

1

You could do it with a Filter. Either homegrown or a 3rd party one like URLRewriteFilter. Just map it on *.xhtml and then forward to /xhtml/*.

Something like:

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

String ctx = request.getContextPath();
String uri = request.getRequestURI();
String viewId = uri.substring(ctx.length(), uri.length());

if (viewId.startsWith("/xhtml")) {
    // Redirect to URL without /xhtml (changes URL in browser address bar).
    response.setStatus(301);
    response.setHeader("Location", ctx + viewId.substring("/xhtml".length());
    // Don't use response.sendRedirect() as it does a temporary redirect (302).
} else {
    // Forward to the real location (doesn't change URL in browser address bar).
    request.getRequestDispatcher("/xhtml" + viewId).forward(request, response);
}

But simpler would be to just change the directory hierarchy to get rid of /xhtml subfolder. Those CSS/JS (and image) files should preferably be placed in a /resources subfolder so that you can utilize the powers of <h:outputStylesheet>, <h:outputScript> and <h:graphicImage> in a proper way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the detailed response. It's good to know I can use a filter, but if the recommended way is to put the XHTMLs directly into `webapp/`, I think that's what we'll do. No need to overcomplicate things... – sleske Dec 22 '11 at 07:58