<?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>Smartkey - Java Software Consultancy &#187; JavaEE</title>
	<atom:link href="http://blog.smartkey.co.uk/tag/javaee/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartkey.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 02 Sep 2010 15:25:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Access the Size of a Collection in a JSP Page Using JSTL-EL</title>
		<link>http://blog.smartkey.co.uk/2009/01/how-to-access-the-size-of-a-collection-in-a-jsp-page-using-jstl-el/</link>
		<comments>http://blog.smartkey.co.uk/2009/01/how-to-access-the-size-of-a-collection-in-a-jsp-page-using-jstl-el/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 14:48:46 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[EL]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=83</guid>
		<description><![CDATA[A common and frustrating problem that many programmers encounter when first working with JSPs is how to access the size of a Collection using the JavaServer Pages Standard Tag Library (JSTL) and the Expression Language (EL). This article discusses this problem and illustrates a best practice solution.
The Problem
The JSTL and EL greatly reduce the amount [...]]]></description>
			<content:encoded><![CDATA[<p>A common and frustrating problem that many programmers encounter when first working with JSPs is how to access the size of a Collection using the JavaServer Pages Standard Tag Library (JSTL) and the Expression Language (EL). This article discusses this problem and illustrates a best practice solution.</p>
<h3>The Problem</h3>
<p>The JSTL and EL greatly reduce the amount of Java code in a JSP and as a result, can be used improve its maintainability. The EL provides a syntax for working with Java objects and allows an object&#8217;s properties to be accessed using a simple dot notation. So, for example, in order to access the size of a collection, we might assume that we could use an expression like this:</p>
<pre class="brush: xml">
${collectionName.size}
</pre>
<p>Unfortunately this will not work. In order to access a property in this way the object to which the property belongs must be implemented as a JavaBean component and it&#8217;s methods must adhere to the conventions described in the JavaBeans specification.</p>
<p>In short, to read the size property of a collection, the EL requires that the collection implements a getSize method; the bad news for us is that no such method exists in the Collections API.</p>
<h3>Hack 1 &#8211; Use Scriptlets</h3>
<p>The most common work-around to this problem is to simply fall back to using Java in a scriptlet in the JSP instead of the EL and to cast the request attribute to a collection:</p>
<pre class="brush: xml">
&lt;%=((Collection)request.getAttribute(&quot;collectionName&quot;)).size()%&gt;
</pre>
<p>This work-around produces some quite ugly code and becomes even worse when we use it as part of another expression; for example, to test the size within an if tag:</p>
<pre class="brush: xml">
&lt;c:if test=&quot;&lt;%= ((Collection) request.getAttribute(&quot;collectionName&quot;)).size() == 1 %&gt;&quot;&gt;
...
&lt;/c:if&gt;
</pre>
<h3>Hack 2 &#8211; Provide a JavaBeans Compliant Collection Wrapper</h3>
<p>A more elegant solution is to wrap the Collection with an object that implements the Collection interface and delegates all calls to the real collection:</p>
<pre class="brush: java">
public class JSTLCollectionWrapper implements Collection {
    //the underlying collection that is being wrapped
    private final Collection realCollection;

    //constructor
    public JSTLCollectionWrapper(Collection c) {
        realCollection = c;
    }

    //implement Collection interface and delegate all calls to realCollection

    //provide a JavaBeans getter method for the size property
    public int getSize() {
        return realCollection.size();
    }
}
</pre>
<p>Then in your controller class, wrap all collections before adding them to the request or the users session:</p>
<pre class="brush: java">
Collection wrapped = new JSTLCollectionWrapper(realCollection);
request.setAttrubute(&quot;collectionName&quot;, wrapped);
</pre>
<p>The JSTL expressions can now access the size property of the collection as the wrapper class implements this according to the JavaBeans specification:</p>
<pre class="brush: xml">

${collectionName.size}
</pre>
<p>The downside to this approach is that it requires you write the wrapper class, and that you must remember to use it wherever you add a collection as an attribute to an HTTP request or session in your controller classes.</p>
<h3>The Best Practice Approach</h3>
<p>Just so long as you&#8217;re using an application server that supports JSTL 1.1 or later (and you really should be), there&#8217;s a much neater solution available: use the functions tags.<br />
The functions tags can be used in a JSP just like any other tag library, but rather than providing tags to use, the library extends the capabilities of the EL by providing some simple functions for you to use. Here&#8217;s how you can check the size of a collection using the functions tags:</p>
<pre class="brush: java">
&lt;%-- declare that we intend to use the functions taglib --%&gt;
&lt;%@ taglib prefix=&#039;fn&#039; uri=&#039;http://java.sun.com/jsp/jstl/functions&#039; %&gt;

&lt;%-- use it --%&gt;
${fn:length(collectionName)}
</pre>
<p>The first line here is just a standard taglib directive telling the application server that we would like to use the functions tags. The second line illustrates how the length function can then be used to return the size of a collection.</p>
<p>As you can see, the syntax is pretty simple, far less intrusive than using a scriptlet, and does not require any special coding in your controller classes. It also has the added advantage that it works for arrays too, and will even return the length of a String should you need to figure that out.<br />
This solution retains much of its simplicity even when used in more complex expressions:</p>
<p>So there you have it. If you&#8217;ve been trying to figure out how to work with collection&#8217;s sizes in your JSPs, or maybe you&#8217;ve just been praying that Sun would rename the size method to getSize, then the functions tag library can offer a neat and simple solution to this common programming pitfall.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2009/01/how-to-access-the-size-of-a-collection-in-a-jsp-page-using-jstl-el/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Adding XML to Javadoc comments</title>
		<link>http://blog.smartkey.co.uk/2008/08/adding-xml-to-javadoc-comments/</link>
		<comments>http://blog.smartkey.co.uk/2008/08/adding-xml-to-javadoc-comments/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 14:04:27 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Javadoc]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JavaSE]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=71</guid>
		<description><![CDATA[In this article I&#8217;ll show you a simple technique for inserting XML code snippets in your Javadocs that keeps them readable in both the source code and the resulting HTML files.
Problems with adding comments
Inserting code snippets into Javadoc comments that read well in both the source code and the HTML files produced by Javadoc can [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I&#8217;ll show you a simple technique for inserting XML code snippets in your Javadocs that keeps them readable in both the source code and the resulting HTML files.</p>
<h3>Problems with adding comments</h3>
<p>Inserting code snippets into Javadoc comments that read well in both the source code and the HTML files produced by Javadoc can be a very fiddly process. Inserting XML snippets into your documentation can make this trickier still. Read on to discover a simple technique that works equally well with both Java and XML code snippets in your Javadocs.</p>
<p>I recently needed to add some comments to my Java classes to show some simple cases of how they should be used in other applications.</p>
<p>In particuar, the classes I was developing were for use in Spring based applications and would need to be configured using XML. This is where the Javadoc tool really starts to struggle. When generating the HTML pages, Javadoc does not (by default) escape the XML characters it finds in your comments &#8211; it just copies them verbatim into the HTML it produces.</p>
<p>So, if you were to write a comment like this:</p>
<pre class="brush: xml">
/**
* &lt;p&gt;This class should be used as follows:&lt;/p&gt;
* &lt;bean&gt;
*     &lt;property name=&quot;propName&quot; value=&quot;propValue&quot;/&gt;
* &lt;/bean&gt;
*/
public class MockObject {
...
}
</pre>
<p>you will only see the following when examining the resulting Javadocs in a browser:</p>
<pre>This class should be used as follows:</pre>
<p>The reason for this is that the markup has been copied directly into the HTML page. The browser doesn&#8217;t recognise the bean tag and simply ignores it.</p>
<h3>Using the {@code &#8230; } Javadoc tag</h3>
<p>Javadoc does provide a useful tag or adding code to comments:</p>
<pre class="brush: xml">
/**
* &lt;p&gt;This class should be used as follows:&lt;/p&gt;
* {@code
* &lt;bean&gt;
*     &lt;property name=&quot;propName&quot; value=&quot;propValue&quot;/&gt;
* &lt;/bean&gt;
* }
*/
public class MockObject {
...
}
</pre>
<p>This will cause the XML within the {@code &#8230; } markup to be escaped so that the &lt; and &gt; characters are displayed properly. The problem with this technique though is that line breaks will be ignored by the browser and all the neatly formatted XML in our source code will be displayed on a single line by a browser.</p>
<h3>Preserving XML Characters and Line Breaks</h3>
<p>In order to preserve the line breaks and the XML characters, you need to use the {@code &#8230; } Javadoc tags in conjunction with HTML pre tags:</p>
<pre class="brush: xml">
/**
* &lt;p&gt;This class should be used as follows:&lt;/p&gt;
* &lt;pre&gt;
* {@code
* &lt;bean&gt;
*     &lt;property name=&quot;propName&quot; value=&quot;propValue&quot;/&gt;
* &lt;/bean&gt;
* }
* &lt;/pre&gt;
*/
public class MockObject {
...
}
</pre>
<p>This will produce the kind of markup we&#8217;re looking for:</p>
<pre>This class should be used as follows:

&lt;bean&gt;
     &lt;property name="propName" value="propValue"/&gt;
&lt;/bean&gt;</pre>
<p>The HTML pre tags instruct the browser to respect whitespace characters and to show the spaces and line breaks. The {@code &#8230; } Javadoc tag instructs the Javadoc tool to escape the &lt; and &gt; characters so that they will also be displayed correctly by the browser too.</p>
<p>So there you have it! A (relatively) simple technique that allows both Java and XML code snippets to look as good in your Java source code as they do in the Javadoc HTML files.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2008/08/adding-xml-to-javadoc-comments/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
