<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>OcpSoft &#187; JSF Facelets Java</title> <atom:link href="http://ocpsoft.com/tags/jsf-facelets-java/feed/" rel="self" type="application/rss+xml" /><link>http://ocpsoft.com</link> <description>&#34;Simple Software&#34;</description> <lastBuildDate>Tue, 27 Jul 2010 15:07:50 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Create a Common Facelets Tag Library: Share it across projects</title><link>http://ocpsoft.com/opensource/create-common-facelets-jar/</link> <comments>http://ocpsoft.com/opensource/create-common-facelets-jar/#comments</comments> <pubDate>Wed, 10 Sep 2008 03:03:43 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[OpenSource]]></category> <category><![CDATA[JSF Facelets Java]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=13</guid> <description><![CDATA[If you've learned to use JSF Facelets to create on-the-fly, simple components using XHTML, then you probably have a whole slew of custom components that need to be copied between various projects, and can be somewhat painful to keep up to date. You may have tried to move them into a jar file, but Facelets can't find them there (without some help from us.)]]></description> <content:encoded><![CDATA[<h2>Tutorial &#8211; Step By Step</h2><p>If you&#8217;ve learned to use JSF Facelets to create on-the-fly, simple components using XHTML, then you probably have a whole slew of custom components that need to be copied between various projects, and can be somewhat painful to keep up to date. You may have tried to move them into a jar file, but Facelets can&#8217;t find them there (without some help from us.)</p><p><em>*The author of this article has requested feedback on the usability of this article. Please post questions, improvements, and/or comments.</em></p><p><span
id="more-13"></span></p><h2>Goals</h2><p>The intent of this tutorial is to explain how to create a packaged jar file, which can be referenced from multiple projects. and which contains all of your tag components and classes for easier maintenance.</p><p><em>(Please note that you may still include non-xhtml based components in this tag library, this does not limit you to use only xhtml facelets.)</em></p><h3>Download the following archive</h3><ul><li>Facelets Jar: jsf-facelets.jar <a
href="https://facelets.dev.java.net/servlets/ProjectDocumentList?folderID=3635&amp;expandFolder=3635&amp;folderID=0" target="_blank">(</a><a
href="https://facelets.dev.java.net/servlets/ProjectDocumentList?folderID=3635&amp;expandFolder=3635&amp;folderID=0" target="_blank">1.1.14 was used in this tutorial)<br
/> </a></li></ul><h2>Instructions:</h2><ul><li>Create a new Java Project. We will call it &#8220;facelets-taglib-common&#8221;<em><br
/> We recommend the title for your project should be the same as your tag library, since this will be the new home of those custom (shiny) components.</em></li><li>Extract the Facelets archive and copy &#8220;jsf-facelets.jar&#8221; into your project. Make sure that it is added to the class path.</li><li>Copy and paste the following source files into your project.</li><li>Create your facelets-taglib.common.xml definition file.</li><li>Make necessary additions to web.xml</li><li>Create your first tags.</li></ul><p>When you are finished with this tutorial, you should have the following directory structure:<br/><br/></p><pre style="padding-left: 150px;"><strong>facelets-taglib-common</strong><strong>/</strong>
+---JavaSource/
|   |   CustomResourceResolver.java
|   -
|
+---lib/
|   |   jsf-facelets.jar
|   -
|
+---META-INF/
|   |   facelets-common-taglib.xml
|   |   facelets-common-taglib.tld
|   |   MANIFEST.MF
|   |
|   +---taglib/
|       |   analytics.xhtml
|       |   doctype.xhtml
|       |   your_custom_tag.xhtml
-       -</pre><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/></p><h2>CustomResourceResolver.java</h2><p>This is a required utility class to allow Facelets to find resources that are not in your project folder, but instead, anywhere on the build path.</p><p>Put this file in your source folder. The reason putting your XHTML custom components and tag XML files does not work out of the box is because Facelets uses a strict ResourceResolver. The default ResourceResolver looks only in the path of your current project / War. Fortunately, however, we can override the default behavior.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.facelets.impl.DefaultResourceResolver</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.facelets.impl.ResourceResolver</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomResourceResolver <span style="color: #000000; font-weight: bold;">extends</span> DefaultResourceResolver <span style="color: #000000; font-weight: bold;">implements</span> ResourceResolver
<span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">URL</span> resolveUrl<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> resource<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">URL</span> resourceUrl <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">resolveUrl</span><span style="color: #009900;">&#40;</span>resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resourceUrl <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resource.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                resource <span style="color: #339933;">=</span> resource.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            resourceUrl <span style="color: #339933;">=</span> <span style="color: #003399;">Thread</span>.<span style="color: #006633;">currentThread</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getContextClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> resourceUrl<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/></p><h2>facelets-taglib-common.xml</h2><p>This file defines your tag-library to facelets, it is required. See the facelets <a
href="https://facelets.dev.java.net/nonav/docs/dev/docbook.html#taglib-create" target="_blank">documentation</a>. After this step, you should be able to package and export your project as a Jar file.</p><p>This file needs to be located in your <em>/facelets-taglib-common/META-INF/</em> directory.</p><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE facelet-taglib PUBLIC
  &quot;-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN&quot;
  &quot;facelet-taglib_1_0.dtd&quot;&gt;
&lt;facelet-taglib&gt;
	&lt;namespace&gt;http://www.ocpsoft.com/facelets-common&lt;/namespace&gt;
	&lt;tag&gt;
		&lt;tag-name&gt;analytics&lt;/tag-name&gt;
		&lt;source&gt;analytics.jspx&lt;/source&gt;
	&lt;/tag&gt;
	&lt;tag&gt;
		&lt;tag-name&gt;doctype&lt;/tag-name&gt;
		&lt;source&gt;doctype.jspx&lt;/source&gt;
	&lt;/tag&gt;
&lt;/facelet-taglib&gt;</pre></div></div><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/></p><h2>facelets-taglib-common.tld <em>(optional)</em></h2><p>This file describes your tag-library for your IDE&#8217;s autocompletion, and for Validation, so that you can check during development to ensure that your tags are being properly used.</p><p><strong>Note**:</strong> To enable autocompletion in your IDE, you probably need to copy this file into your own Web Application&#8217;s <em>WebContent/META-INF/taglib/</em> directory, this does not affect Facelets.</p><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">&lt;!DOCTYPE taglib PUBLIC &quot;-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN&quot; 
&quot;http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd&quot;&gt;
&nbsp;
&lt;taglib xmlns=&quot;http://java.sun.com/JSP/TagLibraryDescriptor&quot;&gt;
	&lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
	&lt;jsp-version&gt;1.2&lt;/jsp-version&gt;
	&lt;short-name&gt;rest&lt;/short-name&gt;
	&lt;uri&gt;http://www.yoursite.com/facelets-taglib-common&lt;/uri&gt;
	&lt;display-name&gt;My Common Tag Library&lt;/display-name&gt;
	&lt;tag&gt;
		&lt;name&gt;analytics&lt;/name&gt;
		&lt;tag-class&gt;common/analytics.jspx&lt;/tag-class&gt;
		&lt;body-content&gt;JSP&lt;/body-content&gt;
		&lt;description&gt;
			Render a google analytics identifier tag.
		&lt;/description&gt;
		&lt;attribute&gt;
			&lt;name&gt;siteId&lt;/name&gt;
			&lt;required&gt;true&lt;/required&gt;
			&lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
			&lt;type&gt;java.lang.String&lt;/type&gt;
			&lt;description&gt;
				Your unique google site-id, eg: UA-4723***-*
			&lt;/description&gt;
		&lt;/attribute&gt;
	&lt;/tag&gt;
	&lt;tag&gt;
		&lt;name&gt;doctype&lt;/name&gt;
		&lt;tag-class&gt;common/doctype.jspx&lt;/tag-class&gt;
		&lt;body-content&gt;JSP&lt;/body-content&gt;
		&lt;description&gt;
			Render an XHTML doctype declaration.
		&lt;/description&gt;
	&lt;/tag&gt;&lt;/taglib&gt;</pre></div></div><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/></p><h2>web.xml</h2><p>We need to make two additions to your Web Application&#8217;s web.xml file in order for this to work. Assuming that you have already <a
href="https://facelets.dev.java.net/nonav/docs/dev/docbook.html#config-faces">installed Facelets</a> into your <em>faces-config.xml</em> file, you also need to do two things here:</p><ol><li>Add your tag library XML file to the list of Libraries that Facelets will load.</li><li>Override the default ResourceResolver</li></ol><p>After this step, you should have a working configuration. Just make sure that your <em>facelets-taglib-common.jar</em> is on the class path, or that you have referenced the new taglibrary project as a dependency.</p><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">&lt;context-param&gt;
	&lt;param-name&gt;facelets.LIBRARIES&lt;/param-name&gt;
	&lt;param-value&gt;
		/META-INF/taglib/facelets-taglib-common.xml
	&lt;/param-value&gt;
&lt;/context-param&gt;
&lt;context-param&gt;
	&lt;param-name&gt;facelets.RESOURCE_RESOLVER&lt;/param-name&gt;
	&lt;param-value&gt;CustomResourceResolver&lt;/param-value&gt;
&lt;/context-param&gt;</pre></div></div><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/></p><h2>Additional Resources:</h2><p>Just in case you&#8217;re wondering how we did doctype and analytics, here is the source:</p><h2>analytics.xhtml</h2><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&nbsp;
&lt;!-- THIS FILE IS LICENCED UNDER THE GPL3
	Authors: 	Derek Hollis
			Lincoln Baxter, III
&nbsp;
	Version:    1.2
	Date:	        2008/05/04
	 --&gt;
&nbsp;
&lt;jsp:root xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;
	xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
	xmlns:c=&quot;http://java.sun.com/jstl/core&quot;
	xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
	xmlns:f=&quot;http://java.sun.com/jsf/core&quot; version=&quot;2.0&quot;&gt;
&nbsp;
	&lt;ui:composition&gt;
		&lt;h:outputText value='&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;'
			escape=&quot;false&quot; /&gt;
		&lt;h:outputText
			value='var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);'
			escape=&quot;false&quot; /&gt;
		&lt;h:outputText
			value='document.write(unescape(&quot;%3Cscript src=&amp;#39;&quot; + gaJsHost + &quot;google-analytics.com/ga.js&amp;#39; type=&amp;#39;text/javascript&amp;#39;%3E%3C/script%3E&quot;));'
			escape=&quot;false&quot;/&gt;
		&lt;h:outputText value='&amp;lt;/script&amp;gt;' escape=&quot;false&quot; /&gt;
&nbsp;
		&lt;h:outputText value='&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;' escape=&quot;false&quot; /&gt;
		&lt;h:outputText value='var pageTracker = _gat._getTracker(&quot;#{siteId}&quot;);' escape=&quot;false&quot; /&gt;
		&lt;h:outputText value='pageTracker._initData();' escape=&quot;false&quot; /&gt;
		&lt;h:outputText value='pageTracker._trackPageview();' escape=&quot;false&quot; /&gt;
		&lt;h:outputText value='&amp;lt;/script&amp;gt;' escape=&quot;false&quot; /&gt;
	&lt;/ui:composition&gt;
&lt;/jsp:root&gt;</pre></div></div><p><br/></p><h2>doctype.xhtml</h2><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&nbsp;
&lt;!-- THIS FILE IS LICENCED UNDER THE GPL3
	Authors: 	Derek Hollis
			Lincoln Baxter, III
&nbsp;
	Version:    $1.0$
	Date:	        2008/05/04
--&gt;
&nbsp;
&lt;jsp:root xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;
	xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
	xmlns:c=&quot;http://java.sun.com/jstl/core&quot;
	xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
	xmlns:f=&quot;http://java.sun.com/jsf/core&quot; version=&quot;2.0&quot;&gt;
&nbsp;
	&lt;ui:composition&gt;
		&lt;h:outputText value='&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;'
			escape=&quot;false&quot; /&gt;
		&lt;h:outputText
			value='&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;'
			escape=&quot;false&quot; /&gt;
	&lt;/ui:composition&gt;
&lt;/jsp:root&gt;</pre></div></div><p><br/></p><h4 style="text-align: center;">&#8212;-</h4><p><br/><br
/> That should be everything. You should now be able to store your common facelets tags and components in a centralized location, allowing for re-use across projects and teams.</p><h3>References</h3><ul><li><a
href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=https%3A%2F%2Ffacelets.dev.java.net%2F&amp;ei=QTLHSO28F4zqvAW58P3BAQ&amp;usg=AFQjCNH9xh-uwQ2iBV3ilrhBxoSEKVfeMQ&amp;sig2=bC8W5yiQ71MoydcbLSziOQ" target="_blank">Facelets Project</a></li></ul> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/create-common-facelets-jar/feed/</wfw:commentRss> <slash:comments>18</slash:comments> </item> </channel> </rss>
<!-- Served from: ocpsoft.com @ 2010-07-29 17:25:51 by W3 Total Cache -->