[jboss-user] [JBoss Seam] - Re: Facelet Templates from a Database

Lundegaard do-not-reply at jboss.com
Thu Jan 31 07:51:41 EST 2008


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 at 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


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4125126#4125126

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4125126



More information about the jboss-user mailing list