views:

41

answers:

1

Hi,

is it possible to load jsf 2 page from database, not from xhtml file? Eg., the request comes for /faces/foo.xhtml, FacesServet intercepts request and VieHanlder creates view foo.xhtml by loading foo.xhtml from a DB, not from the server?

Thanks

A: 

It is in theory possible if you put it from the database into the public webcontent exactly there where the FacesServlet expect it to be, before it kicks in. A Filter is suitable for the job.

Here's a kickoff example:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    String rootPath = req.getSession().getServletContext().getRealPath("/");
    String fileName = req.getServletPath().substring(1);
    File file = new File(rootPath, fileName);

    if (!file.exists()) {
        InputStream input = null;
        OutputStream output = null;

        try {
            input = yourDAO.find(fileName);
            output = response.getOutputStream();
            byte[] buffer = new byte[10240];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException ignore) {}
            if (input != null) try { input.close(); } catch (IOException ignore) {}
        }
    }

    chain.doFilter(request, response);
}

Map this on the <servlet-name> of the FacesServlet.

BalusC