0

In my JSF application, I would like to implement a web filter to change the requested view in function of the used device (I use spring-mobile device resolver).

I have this in my filter:

    String requestURI = request.getRequestURI();

    Device device = DeviceUtils.getCurrentDevice(request);

    if (!requestURI.contains("/mobile") && device.isMobile()) {
        String newUri = requestURI.replace("/contextroot/faces/html/", "/contextroot/faces/html/mobile/");
        request.getRequestDispatcher(newUri).forward(request, response);
    }
    else {
        filterChain.doFilter(request, response);
    }

But I get an exception

/contextroot/faces/html/mobile/consult/consult.xhtml Not Found in ExternalContext as a Resource

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Stéphane
  • 514
  • 5
  • 16

1 Answers1

0

The HttpServletRequest#getRequestDispatcher() takes a path relative to the context root, so you shouldn't include the path of the context root itself. This is clearly specified in the javadoc (emphasis mine):

...

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

...

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555