<?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; JSP</title>
	<atom:link href="http://blog.smartkey.co.uk/tag/jsp/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>Open Session in View Pattern for Spring and JPA</title>
		<link>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/</link>
		<comments>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:13:24 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Open]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Transaction]]></category>
		<category><![CDATA[View]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=676</guid>
		<description><![CDATA[The Open Session in View Pattern is well publicised in Hibernate circles as the best practice approach to presenting data in the Web tier of an application. In this article I'll demonstrate how simple it is to configure a Spring application to do the same thing with the Java Persistence API (JPA).]]></description>
			<content:encoded><![CDATA[<p>The Open Session in View Pattern is a well publicised <a href="https://www.hibernate.org/43.html" target="_blank">design pattern</a> for Hibernate applications and is considered the best practice approach to presenting data within the Web tier of an application. In this article I&#8217;ll demonstrate how simple it is to configure a Spring application to work the same way when JPA is used for the persistence technology. The exact same principles apply for a plain Spring/Hibernate application that does not use JPA.</p>
<h3>What is Open Session in View?</h3>
<p>The Java Persistence API (JPA) allows an object oriented model to be mapped to a relational database. JPA is a standard specification for Java based Object Relational Mapping frameworks &#8211; in order to use JPA an underlying implementation must be available; the most common choice being Hibernate.</p>
<p>Both JPA an Hibernate support lazy loading of data to restrict the number of queries fired off to the database. In general this means that data will be loaded on demand when methods are called, on a loaded object, that require more data to be loaded. In order for this to work, the object that the method is called on must have been loaded by JPA and be part of the current running transaction.</p>
<p>In a Spring application, calls to demarcate transactions are generally handled by the Spring interceptors.Transactions are normally started when a method call is made on a Spring managed object and committed once that method call ends. This means that if a JSP page requests data by calling a transactional method on a Spring managed bean, then it can only access the data in that bean that has already been loaded within that call. Any calls for data that might be loaded lazily will fail because the object is no longer attached to a JPA transaction after that method call has returned.</p>
<p>Hibernate developers solved this problem using the Open Session in View design, which associates the active session (and hence it&#8217;s transaction) with the thread that makes the call. In this design, the transaction will be committed when the thread completed processing the request, rather than when a method call completes. This allows lazily loaded data to be loaded within the JSP page not just within Spring managed objects.</p>
<h3>Providing Request Scoped Transactions</h3>
<p>A standard way to intercept requests in any Java Web application is to use a web filter. Spring provide an out-of-the-box Web filters that implement the Open Session in View design for both Hibernate and JPA. We will see the JPA version here, but the Hibernate one works in just the same way; just change the class name in the configuration file.</p>
<p>In web.xml:</p>
<pre class="brush: xml">
&lt;filter&gt;
    &lt;filter-name&gt;oemInViewFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    &lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;entityManagerFactoryBeanName&lt;/param-name&gt;
        &lt;param-value&gt;reportsEntityManagerFactory&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
    &lt;filter-name&gt;oemInViewFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
<p>The filter class is from the Spring framework libraries. The filter mapping, in this example, applies this filter to all URLs ending with &#8216;.jsp&#8217;; this means that all JSPs that retrieve a lazily loaded object form the service tier of the application are now free to read any data members from it &#8211; any unloaded data will be loaded from the database when calls are made to access it. </p>
<p>Be aware that the filter turns off the auto-flush behaviour of the standard entity manager. This means that any changes that are made to the data in the JSP will not be persisted. The filter can be configured to flush these if requested.</p>
<p>Finally, the init-param that&#8217;s provided to the filter is optional and can be used to explicitly name the entity manager factory that will be used. The example shows how to use an entity manager named &#8216;reportsEntityManagerFactory&#8217; in the Spring configuration files; the default value for this is &#8216;entityManagerFactory&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2010/03/open-session-in-view-pattern-spring-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
	</channel>
</rss>
