<?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...: How to get started with Java ActionPack</title>
    <link>http://bloritsch.d-haven.net/articles/2007/08/06/how-to-get-started-with-java-actionpack</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Random thoughts</description>
    <item>
      <title>How to get started with Java ActionPack</title>
      <description>&lt;p&gt;I&amp;#8217;ve left hints about my Java Actionpack, but there really isn&amp;#8217;t much out there about it for the general world.  So how about I give you a quick primer about how to use it?  First off, you will have to check it out with Subversion.  There&amp;#8217;s a slight conflict with the web site being pulled from &lt;span class="caps"&gt;SVN&lt;/span&gt; and browsing the repository, so you&amp;#8217;ll have to use the &lt;span class="caps"&gt;SVN&lt;/span&gt; client instead of mounting the directory or browsing it online.  I&amp;#8217;ll have to work with my &lt;span class="caps"&gt;ISP&lt;/span&gt; on that.&lt;/p&gt;


&lt;pre&gt;
svn co http://d-haven.org/svn/open/trunk/actionpack
&lt;/pre&gt;

	&lt;p&gt;It&amp;#8217;s world readable, but only authorized people can write.  For now, that&amp;#8217;s just me but I&amp;#8217;ve not really solicited any help at this point.  Since that&amp;#8217;s not the point right now, let&amp;#8217;s worry about how to get it installed and running.  Once you&amp;#8217;ve checked it out, you&amp;#8217;ll need to build it.  Go to the directory where you checked it out and build it with &lt;a href="http://ant.apache.org"&gt;&lt;span class="caps"&gt;ANT&lt;/span&gt;&lt;/a&gt; .  Yes we kept it simple and only used &lt;span class="caps"&gt;ANT&lt;/span&gt;.&lt;/p&gt;


&lt;pre&gt;
ant jar
&lt;/pre&gt;

	&lt;p&gt;The ant script builds two jars, &amp;#8220;java-actionpack.jar&amp;#8221; and &amp;#8220;java-actionapack-test.jar&amp;#8221;.  The first file is what you will include in your webapp to make the magic happen.  The second jar provides the helpers and mocks to help you test the controllers in your application.  I&amp;#8217;m assuming at this point you know how to set up a web application, how to include your code, and configure a &lt;code&gt;web.xml&lt;/code&gt; file.  So I&amp;#8217;m only going to talk about what is specific to Java Actionpack.  Obviously, java-actionpack.jar goes in your &lt;span class="caps"&gt;WEB&lt;/span&gt;-INF/lib folder.  You&amp;#8217;ll need to add the following to your web.xml file to use Java Actionpack:&lt;/p&gt;


&lt;pre&gt;
  &amp;lt;filter&amp;gt;
     &amp;lt;filter-name&amp;gt;ActionPack&amp;lt;/filter-name&amp;gt;
     &amp;lt;filter-class&amp;gt;org.dhaven.actionpack.Dispatcher&amp;lt;/filter-class&amp;gt;
     &amp;lt;init-param&amp;gt;
       &amp;lt;param-name&amp;gt;routes.file&amp;lt;/param-name&amp;gt;
       &amp;lt;param-value&amp;gt;WEB-INF/app.routes&amp;lt;/param-value&amp;gt;
     &amp;lt;/init-param&amp;gt;
     &amp;lt;init-param&amp;gt;
       &amp;lt;param-name&amp;gt;controller.package&amp;lt;/param-name&amp;gt;
       &amp;lt;param-value&amp;gt;com.mycom.app.controllers&amp;lt;/param-value&amp;gt;
     &amp;lt;/init-param&amp;gt;
     &amp;lt;init-param&amp;gt;
       &amp;lt;param-name&amp;gt;environment&amp;lt;/param-name&amp;gt;
       &amp;lt;param-value&amp;gt;development&amp;lt;/param-value&amp;gt; 
&amp;lt;!--       &amp;lt;param-value&amp;gt;test&amp;lt;/param-value&amp;gt; --&amp;gt;
&amp;lt;!--       &amp;lt;param-value&amp;gt;production&amp;lt;/param-value&amp;gt; --&amp;gt;
     &amp;lt;/init-param&amp;gt;
  &amp;lt;/filter&amp;gt;

  &amp;lt;filter-mapping&amp;gt;
    &amp;lt;filter-name&amp;gt;ActionPack&amp;lt;/filter-name&amp;gt;
    &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
  &amp;lt;/filter-mapping&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;The only thing that is critical for you to add in is the &amp;#8220;controller.package&amp;#8221; parameter.  This tells Actionpack where to look for your controllers.  By default it is simply &amp;#8220;controllers&amp;#8221;, but that doesn&amp;#8217;t really make sense for most people.  The default value for &amp;#8220;environment&amp;#8221; is development, and this really is a way to help you to have logic depending on what environment you are in.  The framework will log a message each time a controller is called with an action that doesn&amp;#8217;t exist.  The message won&amp;#8217;t be logged in testing or production.  I&amp;#8217;ve used it to provide a message about the testing data that our client requires but shouldn&amp;#8217;t be there in production.  You can access it in your code by looking at the static variable &lt;code&gt;Controller.environment&lt;/code&gt;, and the value is from the Environment enum.  Finally the &amp;#8220;routes.file&amp;#8221; parameter allows you to override the default location for the routes file.  The default value is what you see in the configuration snippet above.&lt;/p&gt;


	&lt;p&gt;So what happens if you don&amp;#8217;t have a routes file at all?  Java Actionpack will create some wiring for you.  It will provide the following mappings by default:&lt;/p&gt;


&lt;pre&gt;
/{controller}/{action}/{id}
/{controller}/{action}
&lt;/pre&gt;

	&lt;p&gt;That will provide the familiar mapping of what controller you want, which action on that controller, and the option of passing an ID.  The way the routes file works is that the curly brackets refer to a request &lt;em&gt;attribute&lt;/em&gt; .  So why attributes instead of parameters?  Mainly so that the application isn&amp;#8217;t so easily manipulated by anyone who knows how to type &lt;span class="caps"&gt;URL&lt;/span&gt; parameters.  The request attribute &lt;code&gt;controller&lt;/code&gt; specifies which controller you are calling.  The controller is inside the controller package you set up, and it transforms the name so that it capitalizes the first letter and tacks on Controller to the end.  For example if the request is &lt;code&gt;/message/view/34&lt;/code&gt; and you use the configuration above, the class that Actionpack finds is &lt;code&gt;com.mycom.app.controllers.MessageController&lt;/code&gt; .  The &lt;code&gt;action&lt;/code&gt; attribute specifies the method that will be called.  With the example &lt;span class="caps"&gt;URL&lt;/span&gt;, we will call &lt;code&gt;MessageController.view()&lt;/code&gt; .  It&amp;#8217;s up to the &lt;code&gt;view()&lt;/code&gt; method to read the &lt;code&gt;id&lt;/code&gt; attribute and do something with it.  Let&amp;#8217;s implement this example method:&lt;/p&gt;


&lt;pre&gt;
public class MessageController extends Controller {
    public void view() {
        String id = (String)request.getAttribute("id");

        request.setAttribute("message", Messages.findById(id));
    }
}
&lt;/pre&gt;

	&lt;p&gt;You might wonder why I chose to place the message object into the request attribute.  It&amp;#8217;s really up to you where you put it (the RequestWrapper allows you to override request parameters as well), but this works well if you use the Expression Language and JSPs.  You would just refer to the object in the parameter marker like this: &lt;code&gt;${ message.body }&lt;/code&gt; .  This calls the method &lt;code&gt;getBody()&lt;/code&gt; on the message object you passed in to the request attribute.  You&amp;#8217;ll also notice that the request object is available as a class attribute, as well as the context object and the response object.  They follow the same format as you would expect for a &lt;span class="caps"&gt;JSP&lt;/span&gt; class to keep things familiar.  I.e. the names are &amp;#8220;request&amp;#8221;, &amp;#8220;context&amp;#8221;, and &amp;#8220;response&amp;#8221; respectively.&lt;/p&gt;


	&lt;p&gt;So what about the view?  Since Java Actionpack is supposed to be as non-intrusive as possible (yet still opinionated), Java Actionpack will look in the WebApp context for the file called by the controller/action name.  So for our example &lt;span class="caps"&gt;URL&lt;/span&gt; from above, we would look at &amp;#8220;message/view.jsp&amp;#8221;.  The progression is this: it looks for a &lt;span class="caps"&gt;JSP&lt;/span&gt; file (&amp;#8221;.jsp&amp;#8221;), then an &lt;span class="caps"&gt;HTML&lt;/span&gt; file (&amp;#8221;.html&amp;#8221;), and then finally is just passes the url &amp;#8220;message/view&amp;#8221; to the system to process.  If you want your own supercool view technology as a Servlet, then you just have to set it up to react to the &lt;span class="caps"&gt;URL&lt;/span&gt; scheme we just outlined.  Java Actionpack is a filter, but it uses the request dispatcher to pass that &lt;span class="caps"&gt;URL&lt;/span&gt; on.  We favored the &lt;span class="caps"&gt;JSP&lt;/span&gt; technology partly because more people would be familiar with it, and partly because it finally grew up enough to be nice to use.&lt;/p&gt;


	&lt;p&gt;So what happens if Actionpack can&amp;#8217;t do anything with the &lt;span class="caps"&gt;URL&lt;/span&gt;?  Let&amp;#8217;s say the request comes in for &amp;#8220;index.html&amp;#8221; or &amp;#8220;site.css&amp;#8221;, or even &amp;#8220;fantastic/example.pdf&amp;#8221;.  If the &amp;#8220;controller&amp;#8221; attribute doesn&amp;#8217;t match an existing controller, Java Actionpack gets out of the way, and lets your application deal with it as it normally would.  If the controller is matched, but there is no method for the action attribute, Actionpack will process any filters set up in the controller and process the action calling the &lt;code&gt;action_missing(String actionName)&lt;/code&gt; callback (which by default will log what happened in &lt;span class="caps"&gt;DEVELOPMENT&lt;/span&gt; mode).  Finally it will dispatch to the view as it normally would.  It comes in useful when you have to protect a whole bunch of actions that need a user to have a certain role, but some of the pages are static.  Why should you have to create a method if there is nothing to do in it?&lt;/p&gt;


	&lt;p&gt;These are the basics, and there&amp;#8217;s more to it.  But for now, I just wanted to share how to get it downloaded so you can play around with it.  It should feel natural to a Java developer, and natural enough to someone who has experience with Ruby on Rails.  I&amp;#8217;ll have to do a few more focused tutorials with it.&lt;/p&gt;</description>
      <pubDate>Mon, 06 Aug 2007 12:45:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:47699285-b2f7-40e5-a1ab-065dc7e521d2</guid>
      <author>Berin Loritsch</author>
      <link>http://bloritsch.d-haven.net/articles/2007/08/06/how-to-get-started-with-java-actionpack</link>
      <category>java</category>
      <category>actionpack</category>
      <category>install</category>
      <category>use</category>
      <category>howto</category>
      <category>getting</category>
      <category>started</category>
    </item>
  </channel>
</rss>
