1

I'm trying to integrate a JSF (2.2.10) into a NON JSF existing Project. For compliancy to the existing project structure I would like to customize the path where Mojarra searches for the pages without modifying the url (or using external url-rewrite)

Path for accessing JSF pages

http://my.web.app/context/faces/page1.xhtml

Essentially the lookup scheme would change from

webapp\
       templates\
       WEB-INF\
              lib
       page1.xhtml
       page2.xhtml
       etc...

to

mydir_outside_webapp\
                    templates\
                    page1.xhtml
                    page2.xhtml
                    etc...
...
webapp\
       templates\
       WEB-INF\
               lib

I could not find a way to customize JSF to achieve the desired behaviour. Ah, the application is not bundled in a war but is deployed in a directory structure

Thank you for your support!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marco Bettiol
  • 531
  • 3
  • 12

1 Answers1

3

You can use a custom ResourceHandler for this wherein you override createViewResource() method to check in the external folder first.

public class ExternalResourceHandler extends ResourceHandlerWrapper {

    private ResourceHandler wrapped;
    private File externalResourceFolder;

    public ExternalResourceHandler(ResourceHandler wrapped) {
        this.wrapped = wrapped;
        externalResourceFolder = new File("/path/to/external/resources");
    }

    @Override
    public ViewResource createViewResource(FacesContext context, String path) {
        ViewResource resource = super.createViewResource(context, path); // First try local.

        if (resource == null) { // None found? Try external.
            final File externalResource = new File(externalResourceFolder, path);
            if (externalResource.exists()) {
                resource = new ViewResource() {
                    @Override
                    public URL getURL() {
                        try {
                            return externalResource.toURI().toURL();
                        } catch (MalformedURLException e) {
                            throw new FacesException(e);
                        }
                    }
                };
            }
        }

        return resource;
    }

    @Override
    public ResourceHandler getWrapped() {
        return wrapped;
    }

}

To get it to run, register it as follows in faces-config.xml:

<application>
    <resource-handler>com.example.ExternalResourceHandler</resource-handler>
</application>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555