facelets - JSF: Serving Resources From a Jar -


i´m in process create couple of jars packed facelets templates usage through organisation.

stuck in jsf 1.2 functionality not come out of box. stack:

  • jboss eap 5.1
  • seam 2.2
  • richfaces 3.3.3

it seems me need 2 resources:

  1. resource resolver finds faclets resources
  2. a thing serve css , js resources jar

resource resolver seems easy part: http://ocpsoft.org/opensource/create-common-facelets-jar/

the thing streams css / js more complicated: jsf: serving resources jar http://cagataycivici.wordpress.com/2006/05/09/jsf_serving_resources_from_a/

  • phaselistener
  • filter
  • servlet
  • weblets

i use weblets seems project dedicated solve problem. furthermore recommended in cool jsf book bought: "pro jsf , ajax building rich internet components"

the problem can not find stable release in maven repo , no documentation. in example no faclets not used:

https://github.com/werpu/weblets

in article above filter highlighted solution. http://myfaces.apache.org/tomahawk-project/tomahawk/apidocs/org/apache/myfaces/webapp/filter/extensionsfilter.html

sadly not simple task hook in solution. not have time (or knowledge @ moment) :(

possible can use project: http://jawr.java.net/introduction.html jawr aims bundle js , css files.

the project wiki indicates possible: http://jawr.java.net/docs/generators.html see "classpath resource generator" section.

please advice :)

hello load css, js etc jar use following.

package se.lu.ldc.core.servlet;   import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.url; import java.net.urlconnection;  import javax.faces.webapp.facesservlet; import javax.servlet.servletconfig; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import org.apache.log4j.logger;   /**  * servlet för att hämta diverse filer från jar om de finns jaren  *  jpg, png, gif, css, xcss,xml,js  * @author ldc-jha  *  */ @webservlet(name="ldcservlet",loadonstartup=1,urlpatterns={"*.jpg","*.png","*.gif","*.css","*.xcss","*.js"}) public class ldcframeworkservlet extends httpservlet {        //where files in jar.     public final string base_path = "xhtml/framework";      private logger log = logger.getlogger(getclass().getname());      @override     public void init(servletconfig config) throws servletexception     {         system.out.println("init()");         super.init(config);     }     @override     public void doget(httpservletrequest request,              httpservletresponse response) throws servletexception, ioexception {           /* if servlet not mapped path, use request uri */         string path = request.getpathinfo();         if (path == null) {             path = request.getrequesturi().substring(                     request.getcontextpath().length());         }          url resource = thread.currentthread().getcontextclassloader().         getresource(base_path+"/"+path.substring(1));          if (resource == null) {             servletcontext sc = getservletcontext();              string filename = sc.getrealpath(path);             log.info("during load:"+resource+":"+path+":"+filename);             try{                 resource = sc.getresource(path);             }catch(exception e){}             if(resource == null)             {                 response.senderror(404, path + " denied");              }          }         /* failure conditions */         if (path.endswith(".seam")) {             javax.faces.webapp.facesservlet facesservlet = new facesservlet();             facesservlet.service(request, response);              return;         }         if (path.endswith(".class")) {             response.senderror(403, path + " denied");             return;         }          /* find resource */         log.info("looking " + path + " on classpath");          //response.senderror(404, path + " not found on classpath");          log.info("found " + path + " on classpath:"+resource.tostring());          /* check modification date */         urlconnection connection = resource.openconnection();         long lastmodified = connection.getlastmodified();         long ifmodifiedsince = request.getdateheader("if-modified-since");         if (ifmodifiedsince != -1 && lastmodified <= ifmodifiedsince) {             response.setstatus(httpservletresponse.sc_not_modified);             return;         }          /* write response */         response.setcontenttype(getservletcontext().getmimetype(path));         outputstream out = new bufferedoutputstream(                 response.getoutputstream(), 512);         inputstream in = new bufferedinputstream(                 resource.openstream(), 512);         try {             int len;             byte[] data = new byte[512];             while ((len = in.read(data)) != -1) {                 out.write(data, 0, len);             }         } {             out.close();             in.close();             if (connection.getinputstream() != null) {                 connection.getinputstream().close();             }         }      } /* doget() */       @override     public void service(httpservletrequest request, httpservletresponse response)     throws servletexception, ioexception     {         doget(request, response);     }  } 

if want load faclets jar use custom defaultresourceresolver

/**  * används för hämta hämta xhtml filer från jar filen  * @author ldc-jha  *  */ public class classletsresourceresolver extends defaultresourceresolver implements resourceresolver{      public classletsresourceresolver() {         super();     }      private static final string prefix = "/framework/";     private static final string layout = "/layout/";      public string getprefix() {         return prefix;     }      public url resolveurl(string path) {         final string prefix = getprefix();         system.out.println("resolveurl()"+path);         url url = null;         if (path != null && path.startswith(prefix)) {             final string resource = path.substring(prefix.length());             url = getclass().getclassloader().getresource("xhtml/framework/"+resource);         }          if (path != null && path.startswith(layout)) {             system.out.println("layout:"+path);             url = getclass().getclassloader().getresource("xhtml/framework"+path);             system.out.println(url);         }          if(url != null){             return url;         }         else         {             return super.resolveurl(path);         }     } }  

if want load resourceresolver wihtout editing xhtml can this.

@weblistener public class simpleservletcontextlistener implements servletcontextlistener{      private static final logprovider log = logging.getlogprovider(simpleservletcontextlistener.class);       public void contextinitialized(servletcontextevent event){                         event.getservletcontext().setattribute("facelets.resource_resolver","se.lu.ldc.core.reslover.classletsresourceresolver");  } 

}

you might have let seam know faclets well, let me know if there problem. had same idea, , got working packing css, xhtml etc in jar.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -