<?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>We Heart Code &#187; j2ee</title>
	<atom:link href="http://www.weheartcode.com/category/j2ee/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Thu, 12 May 2011 23:17:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Reusing Annotation Based Controllers in Spring MVC</title>
		<link>http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/</link>
		<comments>http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 17:36:52 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[j2ee]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/</guid>
		<description><![CDATA[I was a late comer to the wonderful world of Spring. Seeing that I hate XML, I am very keen on using annotations everywhere! Everything I read said that using annotation based controllers wasn't a good idea because they were hard to re-use due to their very fluid rules for method signatures. The first thing [...]]]></description>
			<content:encoded><![CDATA[<p>I was a late comer to the wonderful world of <a href="http://springframework.org/">Spring</a>. Seeing that I hate XML, I am very keen on using annotations everywhere! Everything I read said that using annotation based controllers wasn't a good idea because they were hard to re-use due to their very fluid rules for method signatures.</p>
<p>The first thing I did in <a href="http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html">Spring MVC</a> was to write the perennial CRUD application. After I was done the code was fairly clean but still contained plenty of boilerplate java code. So I wanted to make it generic so I could churn out CRUD's for maintenance tables.</p>
<p>Goals:</p>
<ol>
<li>Only write code that's specific to the Entity I am dealing with in the controller.</li>
<li>Make use of &lt;Generics&gt; so I don't have to cast stuff like a caveman from 1997.</li>
<li>Have spring figure out URI mapping based on controller names.</li>
<li>Write <strong>zero</strong> bean specific xml.</li>
</ol>
<p><span id="more-45"></span><br />
Anotomy of a CRUD Controller.<br />
Your classic CRUD (Create Update Delete) Controller works with one domain type and needs to do a few things:</p>
<ol>
<li>List items</li>
<li>Create items</li>
<li>Update items</li>
<li>Delete items</li>
</ol>
<p>As far as controller methods go, you'll generally need to do a few things:</p>
<ul>
<li>Set up your backing domain object for your form on creations and edits</li>
<li>Validate and save results from the form on submission.</li>
<li>List items from your data store.</li>
</ul>
<h3>The Old Way</h3>
<p>Let's look at how we'd accomplish <em>listing </em>items with Spring MVC.</p>
<div class="syntax_hilite">
<div id="java-15">
<div class="java">@RequestMapping<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"/employee/list"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ModelMap list<span style="color: #66cc66;">&#40;</span>ModelMap model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006600;">addAttribute</span><span style="color: #66cc66;">&#40;</span>dao.<span style="color: #006600;">selectAll</span><span style="color: #66cc66;">&#40;</span>Employee.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> model;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So this method uses my dao to run a query. (Your DAO Service implementation will be different depending on how you access your database.)</p>
<p>It then takes the results of that query and throws it in the ModelMap. Recall the ModelMap is smart and will come up with the correct attribute name. So if selectAll returns a List of Employees it will be added to the model as employeeList. Great. Now we use the @RequestMapping annotation to set our URL mapping to employee/list.</p>
<h3>The New Way</h3>
<p>Let's look at how we could make this list method generic. We've got two things that explicitly reference our Employee domain object. The URL mapping and the .class we pass to our DAO service.</p>
<p><b>Solving the URL problem</b><br />
If we define a generic list method in our abstract controller one option for configuring the specific url in the extending class would be something like this.</p>
<div class="syntax_hilite">
<div id="java-16">
<div class="java">@RequestMapping<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"department/list"</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> ModelMap list<span style="color: #66cc66;">&#40;</span>ModelMap map<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
super<span style="color: #66cc66;">&#40;</span>map<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>I don't like this method, it assumes the person implementing the super class will know that they must override the list method just to set a request mapping.  A better method would be if Spring was able to infer the URL map from the implementing controller's class name. ControllerClassNameHandlerMapping to the rescue!</p>
<p>Spring's ControllerClassNameHandlerMapping will take a controller's class name and map it to a URL. I like to mix this with the SimpleUrlHandlerMapping to map views the same way.</p>
<p>Here's how I configure these beans:</p>
<div class="syntax_hilite">
<div id="xml-17">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"order"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"1"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"defaultHandler"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If no @Controller match, map path to a view to render; e.g. the<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;/intro&quot; path would map to the view named &quot;intro&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.mvc.UrlFilenameViewController"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Maps all other request URLs to views --&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"urlMapping"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"defaultHandler"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Selects view names to render based on the request URI: e.g. /index<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selects &quot;index&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.mvc.UrlFilenameViewController"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"order"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"2"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>So what all this means is that if someone requests: /department/list it will automatically map to a Controller called DepartmentController and the @RequestMapping annotated method called list. There's no need to define a URL mapping in our @RequestMapping annotation, we'll just define a request type instead.</p>
<h3>Dynamic entity/domain class</h3>
<p>There are many ways we can take care of the problem. The coolest way is to use some neat <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=208860">reflection tricks</a> like so:</p>
<div class="syntax_hilite">
<div id="java-18">
<div class="java">@SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"unchecked"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Class&lt;T&gt; getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>Class&lt;T&gt;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ParameterizedType<span style="color: #66cc66;">&#41;</span> getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getGenericSuperclass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getActualTypeArguments</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So now we have a generic list method:</p>
<div class="syntax_hilite">
<div id="java-19">
<div class="java">@RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ModelMap list<span style="color: #66cc66;">&#40;</span>ModelMap model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006600;">addAttribute</span><span style="color: #66cc66;">&#40;</span>dao.<span style="color: #006600;">selectAll</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> model;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<h3>Creating/Updating</h3>
<p>The normal pattern for doing creating and updating in Spring MVC controllers consists of a few methods.</p>
<p>Methods to display the initial form:<br />
These methods simply show the same form view.</p>
<p>I implemented them like this, we assume the view name from the Domain object (entity)'s name:</p>
<div class="syntax_hilite">
<div id="java-20">
<div class="java">@RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> create<span style="color: #66cc66;">&#40;</span>ModelMap model, WebRequest request<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> update<span style="color: #66cc66;">&#40;</span>ModelMap model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Now before the form is submitted we need to tell Spring to bind our form to a @ModelAttribute. This is the same as a FormBackingObject in classic Spring MVC.</p>
<p>The way it works is any method annotated with @ModelAttribute will be executed and the returned object bound to any matching forms in a view.</p>
<p>In this view I do one of two things. If we're going to be doing a create I return a new Domain object. This domain object may have some default values set to get initially bound in to the form. If we're doing an update I need to look up the Domain  Object from the DAO Service.</p>
<p>Since our @ModelAttribute annotated method will be called on each request I need a way to tell if this is an update request or a create request. An easy way is just to check if our Primary Key parameter is set in the WebRequest.</p>
<p>Here's what a generic @ModelAttribute method looks like:</p>
<div class="syntax_hilite">
<div id="java-21">
<div class="java">@ModelAttribute<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"crudObj"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> T setupModel<span style="color: #66cc66;">&#40;</span>WebRequest request<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> pk = request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span>getPkParam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>pk == <span style="color: #000000; font-weight: bold;">null</span> || org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">commons</span>.<span style="color: #006600;">lang</span>.<span style="color: #006600;">StringUtils</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span>pk<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> getEntityInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span>dao.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <a href="http://www.google.com/search?q=allinurl%3ALong+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Long</span></a>.<span style="color: #006600;">parseLong</span><span style="color: #66cc66;">&#40;</span>pk<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So we need another abstract method called getPkParam that returns the String representing the domain property that the Primary Key is stored in.<br />
If on your database the primary key is always called "id" then you could certainly implement getPkParam to return "id" every time in the superclass.</p>
<p>So far we have implemented methods that do the following in our superclass: Forward a request to the correct form view and setup the model object to make it available for automatic binding in our view.</p>
<p>The next step is to define what happens when a form is submitted. When a form is submitted we'll have to do 2 things, weather and update or an insert we need to Validate the form and persist the Domain object to the database if validation passes.</p>
<p>Due to the @ModelAttribute annotation the model object will now be available in your view as {$crudObj}. Ideally we would make it available based on the type name so the view was more readable, but that's one of the limitations we have when using annotations is the inability to add dynamic values as parameters.</p>
<p>Let's look at a generic save method:</p>
<div class="syntax_hilite">
<div id="java-22">
<div class="java">@RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">POST</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> save<span style="color: #66cc66;">&#40;</span>@ModelAttribute<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"crudObj"</span><span style="color: #66cc66;">&#41;</span> T entity, BindingResult bindingResult<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//validate entity</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; getValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span>entity, bindingResult<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return to form if we had errors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>bindingResult.<span style="color: #006600;">hasErrors</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//merge into datasource</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dao.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>entity<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return to list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">"redirect:/"</span> + ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/list.html"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>We have a new abstract method called getValidator(Object obj,BindingResult res) that will return the validator for your Domain class.</p>
<h3>Implementation</h3>
<p>We now have all the pieces in place.</p>
<p><strong>Finished CrudController&lt;T&gt;</strong></p>
<div class="syntax_hilite">
<div id="java-23">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> CrudController&lt;T&gt; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Bring in a generic DAO that can handle crud operations.</span><br />
&nbsp; &nbsp; @Autowired <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> SimpleDao dao;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> CrudController<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; super<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">protected</span> Validator getValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"unchecked"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Class&lt;T&gt; getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>Class&lt;T&gt;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ParameterizedType<span style="color: #66cc66;">&#41;</span> getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getGenericSuperclass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getActualTypeArguments</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"unchecked"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> T getEntityInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006600;">forName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">newInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AInstantiationException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">InstantiationException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AIllegalAccessException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IllegalAccessException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AClassNotFoundException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassNotFoundException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp;&nbsp; <span style="color: #808080; font-style: italic;">//assume the primary key property is going to be the Entity Class plus Seq</span><br />
&nbsp; &nbsp;&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getPkParam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortNameAsProperty</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"Seq"</span>;<br />
&nbsp; &nbsp;&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ModelMap list<span style="color: #66cc66;">&#40;</span>ModelMap model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006600;">addAttribute</span><span style="color: #66cc66;">&#40;</span>dao.<span style="color: #006600;">selectAll</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> model;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> create<span style="color: #66cc66;">&#40;</span>ModelMap model, WebRequest request<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">GET</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> update<span style="color: #66cc66;">&#40;</span>ModelMap model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">POST</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> save<span style="color: #66cc66;">&#40;</span>@ModelAttribute<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"crudObj"</span><span style="color: #66cc66;">&#41;</span> T entity, BindingResult bindingResult<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//validate entity</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; getValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span>entity, bindingResult<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return to form if we had errors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>bindingResult.<span style="color: #006600;">hasErrors</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/form"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//merge into datasource</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dao.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>entity<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return to list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">"redirect:/"</span> + ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/list.html"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @RequestMapping<span style="color: #66cc66;">&#40;</span>method = RequestMethod.<span style="color: #006600;">POST</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> delete<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> isInsert, @ModelAttribute<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"crudObj"</span><span style="color: #66cc66;">&#41;</span> T entity, BindingResult bindingResult<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//delete</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dao.<span style="color: #006600;">delete</span><span style="color: #66cc66;">&#40;</span>entity<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return to list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">"redirect:/"</span> + ClassUtils.<span style="color: #006600;">getShortName</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">"/list.html"</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @ModelAttribute<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"crudObj"</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> T setupModel<span style="color: #66cc66;">&#40;</span>WebRequest request<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> pk = request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span>getPkParam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>pk == <span style="color: #000000; font-weight: bold;">null</span> || org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">commons</span>.<span style="color: #006600;">lang</span>.<span style="color: #006600;">StringUtils</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span>pk<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> getEntityInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span>dao.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span>getEntityClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <a href="http://www.google.com/search?q=allinurl%3ALong+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Long</span></a>.<span style="color: #006600;">parseLong</span><span style="color: #66cc66;">&#40;</span>pk<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Set up any custom editors, adds a custom one for java.sql.date by default</span><br />
&nbsp; &nbsp; @InitBinder<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> initBinder<span style="color: #66cc66;">&#40;</span>WebDataBinder binder<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a> dateFormat = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"yyyy-MM-dd"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; dateFormat.<span style="color: #006600;">setLenient</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; binder.<span style="color: #006600;">registerCustomEditor</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> CustomSqlDateEditor<span style="color: #66cc66;">&#40;</span>dateFormat, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p>
Now if I want to add another CRUD to my application I simply have to do the following:</p>
<p>Implement my abstract CrudController for the Domain class I want to edit. So let's say we wanted a department controller.</p>
<p>It's definition would look like this:</p>
<div class="syntax_hilite">
<div id="java-24">
<div class="java">@Controller<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DepartmentController <span style="color: #000000; font-weight: bold;">extends</span> CrudController&lt;Department&gt; <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Validator getValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DepartmentValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Next I'd define my validator class, it might look something like this:</p>
<div class="syntax_hilite">
<div id="java-25">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DepartmentValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> supports<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> departmentClass<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// TODO Auto-generated method stub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> Department.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>departmentClass<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> validate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> healthScope, Errors errors<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ValidationUtils.<span style="color: #006600;">rejectIfEmptyOrWhitespace</span><span style="color: #66cc66;">&#40;</span>errors, <span style="color: #ff0000;">"departmentName"</span>,<span style="color: #ff0000;">"departmentName"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><b>View set up</b><br />
If you notice sometimes I pull the view name from the Domain class, this method is obviously assuming a certain directory structure on your view side:</p>
<p>Here's what my view directory looks like:</p>
<div class="syntax_hilite">
<div id="code-26">
<div class="code">WEB-INF<br />
&nbsp; /jsp<br />
&nbsp; &nbsp; /department<br />
&nbsp; &nbsp; &nbsp; form.<span style="">jsp</span><br />
&nbsp; &nbsp; &nbsp; list.<span style="">jsp</span></div>
</div>
</div>
<p></p>
<p>The last thing I'd do is define my jsp's like this: (<em>You can make your jsp's however you want, i'm just showing some example ones for completeness.</em>)</p>
<p><strong>list.jsp</strong><br />
This page just lists our data using the handy <a href="http://displaytag.sourceforge.net/11/">DisplayTag</a> library.</p>
<div class="syntax_hilite">
<div id="java-27">
<div class="java">&lt;html&gt;<br />
&lt;%@ page language=<span style="color: #ff0000;">"java"</span> contentType=<span style="color: #ff0000;">"text/html; charset=EUC-KR"</span><br />
&nbsp; &nbsp; pageEncoding=<span style="color: #ff0000;">"UTF-8"</span>%&gt;<br />
&lt;%@ taglib prefix=<span style="color: #ff0000;">"c"</span> uri=<span style="color: #ff0000;">"http://java.sun.com/jsp/jstl/core"</span>%&gt;<br />
&lt;%@ taglib uri=<span style="color: #ff0000;">"http://displaytag.sf.net"</span> prefix=<span style="color: #ff0000;">"display"</span> %&gt;<br />
&lt;!DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #ff0000;">"-//W3C//DTD HTML 4.01 Transitional//EN"</span> <span style="color: #ff0000;">"http://www.w3.org/TR/html4/loose.dtd"</span>&gt;<br />
&lt;html&gt;<br />
&lt;c:<span style="color: #a1a100;">import url=&quot;/WEB-INF/jsp/header.jsp&quot; /&gt;</span><br />
&lt;body&gt;<br />
&lt;div id=<span style="color: #ff0000;">"content"</span>&gt;<br />
&nbsp; &nbsp; &lt;br /&gt;<br />
&nbsp; &nbsp; &lt;h3&gt;Departments&lt;/h3&gt;<br />
&nbsp; &nbsp; &lt;span <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">"pagebanner"</span>&gt;&lt;a href=<span style="color: #ff0000;">"&lt;c:url value="</span>create.<span style="color: #006600;">html</span><span style="color: #ff0000;">" /&gt;"</span>&gt;<span style="color: #66cc66;">&#91;</span>Create <span style="color: #000000; font-weight: bold;">New</span><span style="color: #66cc66;">&#93;</span>&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;<br />
&nbsp; &nbsp; &lt;display:table name=<span style="color: #ff0000;">"departmentList"</span> requestURI=<span style="color: #ff0000;">"list.html"</span> pagesize=<span style="color: #ff0000;">"25"</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;display:column href=<span style="color: #ff0000;">"update.html"</span>&nbsp; paramId=<span style="color: #ff0000;">"departmentSeq"</span> paramProperty=<span style="color: #ff0000;">"departmentSeq"</span>/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;display:column title=<span style="color: #ff0000;">"Department Name"</span> property=<span style="color: #ff0000;">"departmentName"</span>/&gt;<br />
&nbsp; &nbsp; &lt;/display:table&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;</div>
</div>
</div>
<p></p>
<p><strong>form.jsp</strong><br />
Displays a form for both a new and edit. Notice this pulls from the command object "crudObj" that was set up in the @ModelAttribute("crudObj") annotated method.</p>
<div class="syntax_hilite">
<div id="java-28">
<div class="java">&lt;%@ page language=<span style="color: #ff0000;">"java"</span> contentType=<span style="color: #ff0000;">"text/html; charset=EUC-KR"</span><br />
&nbsp; &nbsp; pageEncoding=<span style="color: #ff0000;">"UTF-8"</span>%&gt;<br />
&lt;%@ taglib prefix=<span style="color: #ff0000;">"c"</span> uri=<span style="color: #ff0000;">"http://java.sun.com/jsp/jstl/core"</span> %&gt;<br />
&lt;%@ taglib prefix=<span style="color: #ff0000;">"form"</span> uri=<span style="color: #ff0000;">"http://www.springframework.org/tags/form"</span> %&gt;<br />
&lt;%@ taglib prefix=<span style="color: #ff0000;">"spring"</span> uri=<span style="color: #ff0000;">"http://www.springframework.org/tags"</span> %&gt;<br />
&lt;%@ taglib prefix=<span style="color: #ff0000;">"fmt"</span> uri=<span style="color: #ff0000;">"http://java.sun.com/jsp/jstl/fmt"</span> %&gt;</p>
<p>&lt;!DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #ff0000;">"-//W3C//DTD HTML 4.01 Transitional//EN"</span> <span style="color: #ff0000;">"http://www.w3.org/TR/html4/loose.dtd"</span>&gt;<br />
&lt;html&gt;<br />
&lt;body&gt;<br />
&lt;div id=<span style="color: #ff0000;">"content"</span>&gt;<br />
&nbsp; &nbsp; Create/Edit Department<br />
&nbsp; &nbsp; &lt;form:form action=<span style="color: #ff0000;">"save.html"</span> commandName=<span style="color: #ff0000;">"crudObj"</span>&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AName+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Name</span></a>:&nbsp; &lt;form:input path=<span style="color: #ff0000;">"departmentName"</span>/&gt; &lt;form:errors path=<span style="color: #ff0000;">"departmentName"</span> cssClass=<span style="color: #ff0000;">"error"</span>/&gt; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &lt;form:hidden path=<span style="color: #ff0000;">"departmentSeq"</span>/&gt;&lt;br /&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=<span style="color: #ff0000;">"submit"</span> value=<span style="color: #ff0000;">"Submit"</span>/&gt;<br />
&nbsp; &nbsp; &lt;/form:form&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
</div>
</div>
<p></p>
<h3>Summary</h3>
<p> Let's review:</p>
<ul>
<li>By using a generic templated base class we reap the benefits of Spring's "configuration by convention" and our controller is more specific and easy to use.</li>
<li>We take advantage of Configuration by Convention again to automatically have meaningful url's without having to explicitly configure them thanks to Spring's ControllerClassNameHandlerMapping.</li>
</ul>
<p>The way I created an abstract controller above was just one way to do it, however I believe I've shown that just because your controller classes don't extend an old-style Spring controller class doesn't mean <b>you</b> can't take advantage of inheritance to make your own life easier, even when using annotations.<!-- ~ --><!-- ~ --></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/&amp;title=Reusing+Annotation+Based+Controllers+in+Spring+MVC" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Del.icio.us" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/&amp;title=Reusing+Annotation+Based+Controllers+in+Spring+MVC" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to digg" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Reusing+Annotation+Based+Controllers+in+Spring+MVC&amp;u=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to FURL" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/&amp;title=Reusing+Annotation+Based+Controllers+in+Spring+MVC" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to reddit" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Technorati" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/&amp;title=Reusing+Annotation+Based+Controllers+in+Spring+MVC" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Reusing Annotation Based Controllers in Spring MVC' to Google Bookmarks" alt="Add 'Reusing Annotation Based Controllers in Spring MVC' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2008/10/16/reusing-annotation-based-controllers-in-spring-mvc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Eclipselink in J2SE RCP Applications</title>
		<link>http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/</link>
		<comments>http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 14:03:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[eclipselink]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/</guid>
		<description><![CDATA[In this article I'll go over integrating EclipseLink into an RCP application. I will not explain every detail of the JPA specification however. The source code this article is partially based off of is currently pending IP Review for inclusion in the official EclipseLink project, however you may download it now from here. You can [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I'll go over integrating EclipseLink into an RCP application. I will not explain every detail of the JPA specification however. The source code this article is partially based off of is currently pending IP Review for inclusion in the official EclipseLink project, however you may <a href="http://weheartcode.com/comics_crud_el.zip">download it now from here</a>. You can follow the inclusion status on the official <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=226361">bug report</a>. If you just want to browse the source, check out the org.eclipse.* projects in <a href="http://weecode.com/EclipseLinkRcpCrud/trunk/">our anon SVN</a>. This example requires the OSGi branch of EclipseLink, which <a href="http://www.eclipse.org/eclipselink/downloads/">you can get here</a>.</p>
<p>It requires a derby database which <a href="http://weheartcode.com/derby_with_comics_db.zip">you can download from here</a>, simply run the startNetworkServer.bat in the bin directory to start it.</p>
<h2>Introduction</h2>
<p><a href="http://www.eclipse.org/eclipselink/">Eclipselink</a> is a part of the Eclipse Project. It's an ORM that implements the <a href="http://java.sun.com/developer/technicalArticles/J2EE/jpa/">JPA 1.0 specification</a>, the main commiters on the project are Oracle employees, and Eclipselink is based on the mature <a href="http://www.oracle.com/technology/products/ias/toplink/index.html">Toplink</a> library, in fact Toplink is being renamed Eclipselink for future releases.</p>
<h3>Why use JPA?</h3>
<p>JPA is the official persistence specification for <a href="http://java.sun.com/products/ejb/">EJB 3</a>, it's not going anywhere and you're going to have an easier time finding other developers who are familiar with the JPA specification. What's even better is the EclipseLink implementation is the JPA 2.0 reference specification, so there's a good chance some features you enjoy in the EclipseLink implementation will be present in the JPA 2 specification as well.</p>
<h3>Why bother with an ORM?</h3>
<p>When you're coding an <a href="http://wiki.eclipse.org/index.php/Rich_Client_Platform">RCP application</a>, chances are you're already passing around Java Beans that represent your data model. If you could have the mapping take place automatically from your database to java beans, it just makes your life that much easier. Once you add the power of libraries like Jface Databinding it's hard to go back to any other method of data access.</p>
<h3>Death to XML</h3>
<p>Thankfully EclipseLink does not require much XML,  the only xml file I create is a persistence.xml that simply lists all my entity classes. A nice editor is included with the JPA Tools plugin that makes managing it simple. All other configuration can be done via annotations.</p>
<p>Tons more after the Jump!<br />
<span id="more-44"></span></p>
<h2>High Level Plug-in Architecture</h2>
<p>I like to split my project into 2 main parts. You could put it all in one plugin, but this is how I do it.</p>
<h3>UI</h3>
<p>This plugin contains all my UI code. Views, Perspectices, Editors, etc. No data access code should occur here though. Instead we'll just use our model plugin. This isn't an RCP tutorial, so I'm going to assume you know your way around the base RCP classes and project set up.</p>
<h3>The Model</h3>
<p>The model plugin is where all the data access work takes place, from here we can separate 3 main concerns.</p>
<p><strong>Connection Set Up (EntityManagerFactory)</strong><br />
The EclipseLink class that takes care of creating database connection, configuring pooling, or re-using an exisiting datasource as a connection is the EntityManagerFactory.</p>
<p>Configuring your EntityManagerFactory involves specificing the connection, and setting options. Once we're done, you guessed it, we can create EntityManagers!</p>
<p>The call to createEntityManagerFactory takes the name of your "persistence unit", it must match the name you put in your persistence.xml, so in this case it's "comics".</p>
<p>Here's the EntityManagerFactory I created for the Comics example:</p>
<div class="syntax_hilite">
<div id="java-38">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> ComicsEntityManagerFactory <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> EntityManagerFactory emf = <span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> Map&lt;String, Object&gt; properties = <span style="color: #000000; font-weight: bold;">new</span> HashMap&lt;String, Object&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">TARGET_DATABASE</span>, <span style="color: #ff0000;">"Derby"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_DRIVER</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">"org.apache.derby.jdbc.ClientDriver"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_URL</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">"jdbc:derby://localhost:1527/sample;create=true"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_USER</span>, <span style="color: #ff0000;">"app"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_PASSWORD</span>, <span style="color: #ff0000;">"app"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_READ_CONNECTIONS_MIN</span>, <span style="color: #ff0000;">"1"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">JDBC_WRITE_CONNECTIONS_MIN</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">"1"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">BATCH_WRITING</span>, <span style="color: #ff0000;">"JDBC"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">CLASSLOADER</span>, ComicsEntityManagerFactory.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getClassLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"eclipselink.logging.level"</span>, <span style="color: #ff0000;">"FINE"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"eclipselink.logging.timestamp"</span>, <span style="color: #ff0000;">"false"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"eclipselink.logging.session"</span>, <span style="color: #ff0000;">"false"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"eclipselink.logging.thread"</span>, <span style="color: #ff0000;">"false"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; emf = <span style="color: #000000; font-weight: bold;">new</span> PersistenceProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">createEntityManagerFactory</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"comics"</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; properties<span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Creates a new ComicsEntityManager object.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @return the entity manager<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> EntityManager createEntityManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>emf == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> emf.<span style="color: #006600;">createEntityManager</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>Models (EntityManager Usage)</strong><br />
Once we have an EntityManagerFactory we can use that EntityManagerFactory to create an EntityManager. An EntityManager has it's own cache, handles all data access (selects/inserts/updates) and also can manage your transactions (commit/rollback).</p>
<p>I've found that I wrote the same code quite a bit: Obtain an EntityManager from my EntityManagerFactory, begin a transaction, commit a transaction, etc. Rather than write this same code over and over again I find it much easier to simply put this code in a base "Model" class.</p>
<p>I use two types of "Models", one type is your standard Read Only model, I call this class BaseModel, it has some convience methods like selectAll and getNamedQueryResults. The next type of Model I call an "EditorModel" (Because I mostly use it in editors). It has transaction management methods as well as a method called isDirty which can tell me if I have any changes pending to go in the current transaction. This is very useful for editors  and any other place you want to do read/write stuff.</p>
<p>I either use these two classes on their own or I extend them to put business specific methods in them -- things like getAllEmployees(), etc.</p>
<p><strong>Entities</strong><br />
An entity represents a persistent object in your database (usually a table or a view). In java terms they are  POJO's (Plain Old Java Objects). I usually add<a href="http://java.sun.com/docs/books/tutorial/javabeans/properties/bound.html"> property change support</a> to them for use with the <a href="http://wiki.eclipse.org/index.php/JFace_Data_Binding">JFace Databinding</a> library, but that's not required.</p>
<p>Example Entity (this one implements property change support, that's why I'm extending BaseEntityObject) :</p>
<div class="syntax_hilite">
<div id="java-39">
<div class="java">@<a href="http://www.google.com/search?q=allinurl%3AEntity+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Entity</span></a><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Publisher <span style="color: #000000; font-weight: bold;">extends</span> BaseEntityObject <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?q=allinurl%3ASerializable+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; @Id<br />
&nbsp; &nbsp; @GeneratedValue<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> id;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name;</p>
<p>&nbsp; &nbsp; @OneToMany<span style="color: #66cc66;">&#40;</span>mappedBy = <span style="color: #ff0000;">"publisher"</span>, cascade = CascadeType.<span style="color: #006600;">PERSIST</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List&lt;Title&gt; titles;</p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Instantiates a new publisher.<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Publisher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Instantiates a new publisher.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @param name the name<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Publisher<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> = name;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Gets the name.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @return Returns the name.<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Sets the name.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @param name The name to set.<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setName<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> != <span style="color: #000000; font-weight: bold;">null</span> || name != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firePropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"name"</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> = name<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Gets the id.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @return Returns the id.<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getId<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> id;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Sets the id.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @param id the new id</p>
<p>&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setId<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; firePropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"id"</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">id</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">id</span> = id<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* (non-Javadoc)<br />
&nbsp; &nbsp;&nbsp; * @see java.lang.Object#toString()<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Gets the titles.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @return the titles<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List&lt;Title&gt; getTitles<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> titles;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Sets the titles.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @param titles the new titles<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setTitles<span style="color: #66cc66;">&#40;</span>List&lt;Title&gt; titles<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">titles</span> != <span style="color: #000000; font-weight: bold;">null</span> || titles != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firePropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"titles"</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">titles</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">titles</span> = titles<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Rememeber: Every entity defined in your model project must be referenced in your persistence.xml.</p>
<p><strong>Generating Entities</strong><br />
The best way to generate your entities is with the <a href="http://www.eclipse.org/webtools/dali/main.php">Dali JPA Tools plugin</a>, it's available on the standard eclipse update site. After you get Dali make sure you configure your drivers &#038; connections for whatever platform your database uses - oracle,mysql, etc. </p>
<p>You cannot just use the JPA Tools plugin on any ol' project however. Though in the future this limitation will go away. First you must create a special "JPA Project" and immediately converting it to a plugin project. </p>
<p>Then you can then generate your entities by right clicking <strong> on your project</strong> and selecting JPA Tools->Generate Entities.</p>
<p><strong>Common Gotcha - sharing Entities</strong><br />
When we're talking about OSGi, Entities can not pass the class loader boundry, so you cannot (at this time: September 2008) re-use entities between bundles. This just means the EntityManager you persist an entity with, must be in the same bundle. So I couldn't take common entities and put them in their own bundle, then expect to persist/select to them with an EntityManager defined in another bundle.</p>
<p>This isn't as much of a deal breaker as it sounds at first, you just have to violate DRY for Entity Classes, which aren't all that interesting in the first place. There's nothing stopping you from defining common interfaces or Abstract classes that entities from multiple bundles share.</p>
<h3>Selects/Inserts/Updates/Deletes</h3>
<p>So how do you <em>do stuff</em> with EclipseLink? Turns out, it's pretty simple. However there are so many different ways to do things, I don't have the room here to go over it. Please read this <a href="http://wiki.eclipse.org/Developing_Applications_Using_EclipseLink_JPA_(ELUG)">excellent wiki article</a> to quickly get up to speed. Also any JPA Tutorial will get you caught up on the basics.  I'd like to go over how all of this stuff pertains to your run of the mill RCP applications.</p>
<h3>Editor Use Case</h3>
<p>A common usage scenario with a database driven app is to use a FormEditor to modify a collection of entities. Thanks to EclipseLink we get lots of stuff for free, let's take a look at an example editor class.</p>
<p><strong>Editor Initialization</strong><br />
The init method, here we want to get whatever entities we want to set up our Model, remember our Model basically wraps our EntityManager and contains some convenience methods for dealing with transactions, etc. We need to obtain all of our entities from the entity manager, usually through a simple select query.</p>
<div class="syntax_hilite">
<div id="java-40">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span>IEditorSite site, IEditorInput input<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> PartInitException <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PublishersEditorInput myInput = <span style="color: #66cc66;">&#40;</span>PublishersEditorInput<span style="color: #66cc66;">&#41;</span> input;<br />
&nbsp; &nbsp; &nbsp; &nbsp; model = <span style="color: #000000; font-weight: bold;">new</span> PublisherEditorModel<span style="color: #66cc66;">&#40;</span>myInput.<span style="color: #006600;">getPublisher</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; publisher = model.<span style="color: #006600;">getPublisher</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; setSite<span style="color: #66cc66;">&#40;</span>site<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; setInput<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So we create a new PublisherEditorModel, which is a simple class I created that handles starting my transaction, then we get our Publisher entity from the model, let's peak inside the PublisherEditorModel class, here we use call the find method on our entity manager, that method takes the class of our Entity and it's primary key. </p>
<div class="syntax_hilite">
<div id="java-41">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> Publisher getPublisher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; publisher = em.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span>Publisher.<span style="color: #000000; font-weight: bold;">class</span>, publisher.<span style="color: #006600;">getId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> publisher;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So now we've got our Publisher Entity, but it's one we obtained from our EntityManager, that means any changes we make to the entity will be followed by the EntityManager. So if we changed a field, then called save on the EntityManager, an update statement would be sent to the database!</p>
<p>You could of course skip the convention of a model class all together and deal with getting the EntityManager directly from your plugin's EntityManagerFactory yourself, but it's much easier to just write that code once.</p>
<p><strong>Checking for changes</strong><br />
One feature of an editor is a dirty state -- you change something, the editor is marked as dirty (A star is added to the editor title). If you save the changes, they go through, if not they're discarded. By using EclipseLink we get this functionality for free. No matter how complicated our model is. In basically one line of code we take care of what used to be a gigantic chore. Now figuring out exactly what the differences are is a bit more complicated, but for our case this works great.</p>
<div class="syntax_hilite">
<div id="java-42">
<div class="java">@Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> isDirty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; UnitOfWork uow = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>EntityManagerImpl<span style="color: #66cc66;">&#41;</span> model.<span style="color: #006600;">getEntityManager</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getUnitOfWork</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> uow.<span style="color: #006600;">hasChanges</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>Saving Changes</strong><br />
To save our changes we simply commit our transaction, then start a new one (so editing can continue). Once again, we haven't had to write a single line of SQL. This simple commit will handle, updates, inserts, deletes!  This step used to cause me many hours of grief for each new editor, troubleshooting what sort of SQL I had to generate, and in what order. Don't get me wrong, sometimes EclipseLink doesn't generate the SQL you think it should, but it's usually fairly easy to debug what went wrong.</p>
<div class="syntax_hilite">
<div id="java-43">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> doSave<span style="color: #66cc66;">&#40;</span>IProgressMonitor monitor<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006600;">getEntityManager</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006600;">getEntityManager</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">begin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; firePropertyChange<span style="color: #66cc66;">&#40;</span>PROP_DIRTY<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So, our entities we got from our entity manager we expose to our FormPages in the editor, we then bind those very entities to UI controls, either using JFace databinding or by hand -- whichever way you prefer.</p>
<p><strong>Review</strong><br />
That's really all there is to tying a simple editor to a database. Let's review:</p>
<ol>
<li>Obtain an EntityManager and begin a transaction</li>
<li>Obtain all the entities you need from the EntityManager, either through the find method, a JPQL query or a Native SQL Query, or combination of these methods.</li>
<li>Leverage EclipseLink's UnitOfWork class in your isDirty method.</li>
<li>In your form pages, bind Entities to UI controls.</li>
<li>Commit your transaction when save is called.</li>
</ol>
<h3>Caching in a Thick Client</h3>
<p>Traditionally one of the major benefits you get when using an ORM library and with EclipseLink especially is the ability to have a shared in memory cache. The benefits in a server side web application are immense, when we're dealing with a client side thick application. Caching just gets in our way in most cases. The reason for this is obvious: In a web application EntityManagers across users sessions can all easily share the same cache, since they all reside on the same server (usually).</p>
<p> However in a thick client each user is an island. When I make an update to the database, I update my very own Shared Cache, however Mary who is sitting 2 cubes over still has her own cache that's oblivious to my changes. It's imperative that she receive fresh data all the time!</p>
<p><strong>Fresh data from a find call</strong><br />
Calling find on the EntityManager is the easiest way to get a handle on an Entity. However, you're not gauranteed to hit the database on this call. After our find we just need to call refresh to refresh the Entity from the database. I created a method called findFresh that I put in my Base Model class, here's how it looks:</p>
<div class="syntax_hilite">
<div id="java-44">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> &lt;T&gt; T findFresh<span style="color: #66cc66;">&#40;</span>Class&lt;T&gt; entityClass, <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> primaryKey<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; T result =&nbsp; getEntityManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span>entityClass, primaryKey<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>result == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; getEntityManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">refresh</span><span style="color: #66cc66;">&#40;</span>result<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>In EntityManager Setup</strong><br />
You can turn off the Shared Cache EntityManager wide, to stop regular JPQL queries from hitting the shared cache, this is called putting your EntityManager in "Isolated Mode", just set it in the properties map you pass to the createEntityManagerFactory method.</p>
<p>I just set the property like this:</p>
<div class="syntax_hilite">
<div id="java-45">
<div class="java">properties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>PersistenceUnitProperties.<span style="color: #006600;">CACHE_SHARED_DEFAULT</span>, <span style="color: #ff0000;">"false"</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p><strong>Fresh results from Queries</strong><br />
Just to be sure when I execute any Query - Native or otherwise I set the Query Hint to not use the cache, I created a little helper method I through in a util class like this:</p>
<div class="syntax_hilite">
<div id="java-46">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> setNoCacheHints<span style="color: #66cc66;">&#40;</span>Query q<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; q.<span style="color: #006600;">setHint</span><span style="color: #66cc66;">&#40;</span>QueryHints.<span style="color: #006600;">REFRESH</span>, HintValues.<span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>When dealing with NamedQueries you can use the @CacheHint annotation to set the hint.</p>
<h2>Conclusion</h2>
<p>Before using EclipseLink in my business critical RCP applications, we were using all home grown DAO. We successfully ported our old applications to EclipseLink in a matter of months and that was using alpha code. Looking back all the developers agree we couldn't live with out it.  It's amazing what the simplicity of being able to focus on your actual business logic and not SQL generation can do to you, coding is fun again. Thanks EclipseLink!</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/&amp;title=Eclipselink+in+J2SE+RCP+Applications" title="Add 'Eclipselink in J2SE RCP Applications' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Eclipselink in J2SE RCP Applications' to Del.icio.us" alt="Add 'Eclipselink in J2SE RCP Applications' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/&amp;title=Eclipselink+in+J2SE+RCP+Applications" title="Add 'Eclipselink in J2SE RCP Applications' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Eclipselink in J2SE RCP Applications' to digg" alt="Add 'Eclipselink in J2SE RCP Applications' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Eclipselink+in+J2SE+RCP+Applications&amp;u=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/" title="Add 'Eclipselink in J2SE RCP Applications' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Eclipselink in J2SE RCP Applications' to FURL" alt="Add 'Eclipselink in J2SE RCP Applications' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/&amp;title=Eclipselink+in+J2SE+RCP+Applications" title="Add 'Eclipselink in J2SE RCP Applications' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Eclipselink in J2SE RCP Applications' to reddit" alt="Add 'Eclipselink in J2SE RCP Applications' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/" title="Add 'Eclipselink in J2SE RCP Applications' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Eclipselink in J2SE RCP Applications' to Technorati" alt="Add 'Eclipselink in J2SE RCP Applications' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/&amp;title=Eclipselink+in+J2SE+RCP+Applications" title="Add 'Eclipselink in J2SE RCP Applications' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Eclipselink in J2SE RCP Applications' to Google Bookmarks" alt="Add 'Eclipselink in J2SE RCP Applications' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2008/08/27/eclipselink-in-j2se-rcp-applications/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Protototype AJAX &amp; JSON with J2EE and Struts</title>
		<link>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/</link>
		<comments>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/#comments</comments>
		<pubDate>Wed, 16 May 2007 20:27:58 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[struts]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/</guid>
		<description><![CDATA[We don't always get to use the latest greatest web framework, but just because you're using a crusty old dinosaur J2EE XML framework from the pits of hell, doesn't mean you can't make girls cry with your AJAX/JSON skills. This tutorial will show how to use prototype to return JSON data to your jsp. Prerequisites [...]]]></description>
			<content:encoded><![CDATA[<p>We don't always get to use the latest greatest web framework, but just because you're using a crusty old dinosaur J2EE XML framework from the pits of hell, doesn't mean you can't make girls cry with your AJAX/JSON skills.</p>
<p>This tutorial will show how to use prototype to return JSON data to your jsp.<br />
<span id="more-18"></span></p>
<h3>Prerequisites</h3>
<p>This tutorial will be Struts-centric, but really is adaptable to any Java/J2EE platform. I'm not going to include any DB Layer specific code since that seems to change so much, but you should know how to take a query result and put it in a hash map.</p>
<h3>The Goal</h3>
<p>Create a Struts Action class that queries the database and returns the result in a JSON object back to a JSP with as little fuss as possible.</p>
<h3>The JSP</h3>
<p>In this example, I want to populate a text box in my form with data returned from an AJAX request in the background. For example, sometimes you want to pull out a person's name based on some internal ID. This saves our user from having to type it in by hand and also eliminates spelling mistakes.</p>
<p>So let's assume we have a text box where the user would type in the id as well as a text box where they would type the full name, the code would look something like this</p>
<div class="syntax_hilite">
<div id="html-51">
<div class="html"><span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"theId"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"theId"</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"fullName"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"fullName"</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</div>
</div>
<p></p>
<p>We'll then have a javascript function that uses <a href="http://www.prototypejs.org">prototype</a> to create an AJAX request that hits a struts action to to the database look up, then populates the fullName text box with the fullName property from the returned JSON object.</p>
<div class="syntax_hilite">
<div id="javascript-52">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> lookup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;html:rewrite page=&quot;/util/namelookup.do&quot;/&gt;'</span>, <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;parameters: <span style="color: #66cc66;">&#123;</span>theId: $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'theId'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; onSuccess: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>transport, json<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>json.<span style="color: #006600;">executeError</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>json.<span style="color: #006600;">executeError</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fullName'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = json.<span style="color: #006600;">fullName</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h3>The Java</h3>
<p>Our goal here is to do some work and put that work in a JSON object, for me the easiest way is to just query the DB and throw the results into a hashmap or java bean. To do the JSON lifting in this example I use the open source <a href="http://json-lib.sourceforge.net/">json-lib</a>. If an error occurs we'll put error text in a key in the JSON object called executeError so that we can display that in our javascript.</p>
<div class="syntax_hilite">
<div id="java-53">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameLookupAction <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=allinurl%3AAction+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Action</span></a><br />
<span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #66cc66;">&#40;</span>ActionMapping mapping, ActionForm form,<br />
&nbsp; &nbsp; HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a><br />
&nbsp; <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> theId = request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"theId"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Do your database work here to grab the name and populate our HashMap hm</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//In this example I'll just hard-code the name though.</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a> hm = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;hm.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"fullName"</span>,<span style="color: #ff0000;">"Joe Blow"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//each key from our hash map becomes a key in our JSON object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = JSONObject.<span style="color: #006600;">fromObject</span><span style="color: #66cc66;">&#40;</span>hm<span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Plop it in the header so prototype can grab it.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"X-JSON"</span>, json.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a> hm = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; hm.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"executeError"</span>, <span style="color: #ff0000;">"Couldn't find the Full Name because an error occured."</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; JSONObject json = JSONObject.<span style="color: #006600;">fromObject</span><span style="color: #66cc66;">&#40;</span>hm<span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"X-JSON"</span>, json.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006600;">findForward</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"success"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So that's all there is to it, as you can see it's pretty simple stuff but quite powerful. I like to create utility classes that return a whole bunch of info from a query, they're quite handy to have around to quickly display information in your web app without having to do a lot of leg work of new windows and new jsp pages, etc.</p>
<h3>Extra struts-config.xml Stuff</h3>
<p>If you're using struts you need to forward to a jsp of some sort, this is just going to be an empty JSP page, the JSON info is stored in the response header, anyway here's what your struts-config.xml entry might look like for this action.</p>
<div class="syntax_hilite">
<div id="xml-54">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/util/namelookup"</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">"request"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"com.yourcompany.NameLookupAction"</span> <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">"false"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"success"</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/WEB-INF/jsp/ajax/empty.jsp"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/action<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts&amp;u=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

