This content has been marked as final.
Show 3 replies
-
1. Re: Facelet Templates from a Database
tom_goring Jan 31, 2008 4:28 AM (in response to tom_goring)Hi,
Any idea on this as I'm a bit stuck.
My requirement is that users can define the dynamic content of an email and I'd like to use the JSF tags for handling tables etc. So I want to store the facelet in the database.
Any pointers appreciated.
Thanks
tom -
2. Re: Facelet Templates from a Database
nickarls Jan 31, 2008 4:45 AM (in response to tom_goring)From the department of long-shorts:
have the function return a resource-URL like "http://context/resource?id=" + resource
and that servlet would fetch the resource from DB and spit it out.
As I said, there are probably much more elegant ways of doing this... -
3. Re: Facelet Templates from a Database
lundegaard Jan 31, 2008 7:51 AM (in response to tom_goring)I am using a similar code to access templates from another webapp, but the sample DatabaseResourceResolver shows you the basis:
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import com.sun.facelets.impl.DefaultResourceResolver; /** * @author Thomas Maerz (thomas.maerz@primedo.com) */ public class DatabaseResourceResolver extends DefaultResourceResolver { @Override public URL resolveUrl(String path) { URL url = super.resolveUrl(path); if (url != null) { return url; } try { return new URL("internal", null, 0, path, getHandler(path)); } catch (MalformedURLException e) { } return null; } private static URLStreamHandler getHandler(final String path) { return new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { return new URLConnection(url) { @Override public void connect() throws IOException { } @Override public InputStream getInputStream() throws IOException { // Connect to Database and return your content return new ByteArrayInputStream(new byte[] {}); } }; } }; } }
web.xml<context-param> <param-name>facelets.RESOURCE_RESOLVER</param-name> <param-value>DatabaseResourceResolver</param-value> </context-param>
Facelets file<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" template="/cms/some/path/index.html">
But there are some pitfalls:
- Facelets will cache the the returned URL with the path as key
- You have to handle reloading yourself (Override getLastModified() in URLConnection)
- return valid XML
Hope that helps,
Tom