<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Just a Thought...: Helper classes in Java ActionPack: Design Decisions</title>
    <link>http://bloritsch.d-haven.net/articles/2007/10/31/helper-classes-in-java-actionpack-design-decisions</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Random thoughts</description>
    <item>
      <title>Helper classes in Java ActionPack: Design Decisions</title>
      <description>&lt;p&gt;One of the nice features of Ruby on Rails is the ability to mix in helper classes with your controller.  This works seamlessly because of the design of the Ruby language.  When you try the same approach in Java it&amp;#8217;s just plain impossible.  For one thing, Java has no way to mix things in properly.  You could have a setter/getter for the helper class, but you run into type issues if you want to do it automatically.  Either you have to explicitly cast all helpers when you use them, or you have to rely on Java&amp;#8217;s broken generics approach.  It&amp;#8217;s twofold the problem because the controller needs to access the helper, and the helper also needs to access the controller.&lt;/p&gt;


	&lt;p&gt;Now, Ruby on Rails uses the same helper class mixed in to the controller as well as into the view.  At this stage in the game, I think that&amp;#8217;s pretty much a pipe dream.  I mean, it&amp;#8217;s possible to add the helper class instance to a &amp;#8220;helper&amp;#8221; request attribute, but &lt;span class="caps"&gt;JSTL&lt;/span&gt; and &lt;span class="caps"&gt;JSP EL&lt;/span&gt; doesn&amp;#8217;t allow you to call methods with parameters so it&amp;#8217;s kind of a moot point.  I&amp;#8217;d like to fix these limitations in an Java ActiveView project, but that&amp;#8217;s in the future.&lt;/p&gt;


	&lt;p&gt;Without worrying about all the niceties of the way I would prefer to work, I still need to work within the constructs and design mindset of Java.  Sometimes the straitjacket forces you to be creative, but I can&amp;#8217;t think of any clean way that will work without hijacking the compiler.  That&amp;#8217;s not really an option because it creates the need for tools just to work with the framework.  It&amp;#8217;s highly invasive, and it would make me less likely to use the tool&amp;#8212;and I&amp;#8217;m the one writing it.&lt;/p&gt;


	&lt;p&gt;The short term solution is to make all the protected support methods in the controller public.  That allows users to pass the controller to the helper class, and the helper can invoke whatever methods it needs.  That also means that I need to make the &amp;#8220;layout&amp;#8221; property have setters and getters.  Perhaps a medium term solution would be to create a Request Package that included the request object, session object, response object, and all the helper methods to manipulate them (like &amp;#8220;redirectToAction&amp;#8221; and &amp;#8220;sendMessage&amp;#8221;).  The controller would either use this object directly, or the same methods would simply delegate to the request package object.  That object would then be used for both the helper and the controller. That will allow you to have a series of static methods that consume the request package or the controller directly.  It&amp;#8217;s up to you how you want to do it.&lt;/p&gt;


	&lt;p&gt;I haven&amp;#8217;t really finished my decision process, but I&amp;#8217;m all for any ideas that any of you may want to throw my way.&lt;/p&gt;</description>
      <pubDate>Wed, 31 Oct 2007 17:49:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4228032b-8279-46d7-b065-1c51059c2263</guid>
      <author>Berin Loritsch</author>
      <link>http://bloritsch.d-haven.net/articles/2007/10/31/helper-classes-in-java-actionpack-design-decisions</link>
      <category>design</category>
      <category>java</category>
      <category>actionpack</category>
      <category>controllers</category>
      <category>helpers</category>
      <category>delimma</category>
    </item>
  </channel>
</rss>
