20061010 Tuesday October 10, 2006

Adding Routes to the Framework

It wasn‘t hard adding routes to the framework, and of course I followed the Ruby on Rails lead. The goal is to avoid having to write a whole bunch of redundant code just because. I created “Route“ objects which took care of matching URLs and populating the Request object. Each Route object is initialized with a String representation of the URL fragment it responds to and an optional Map of default request attributes. For example, you can create a Route that responds to the root of the context “/“ and pass in default request attributes to specify the controller and action. Alternatively, you can have parts of the URL match a named attribute. For example the common url spec “/{controller}/{action}“ will match the browser provided URL “/user/login”, setting up the controller to be the UserController class (see my previous post) and the action to be the login() method. It‘s a much more flexible API, and it actually produces cleaner code.

The next part of the agenda was to set up the interceptor filters. The controller can be set up so that certain methods are called before or after other methods. While the traditional Ruby implementation allowed you to do all kinds of fun things with the parameters, Java just isn‘t as flexible. As a result I added several methods such as beforeFilter(filterName), beforeFilterOnlyFor(filterName, actionNames...), beforeFilterExceptFor(filterName, actionNames...) and the “afterFilter“ equivalents. The Filters also specify a specific interface with methods aptly named “before“ and “after“. The framework includes a default implementation that uses reflection to call the filter methods to satisfy the beforeFilter and afterFilter variants. It keeps the code very clean.

Next on my agenda is to add the beforeAndAfter filter methods that accept the traditional Filter interface. I like the flexibility of the Ruby implementation where there is no explicit interface. As long as an object has a “before()“ method and an “after()“ method, it can be used as a filter. I just may continue using that, and keep the Filter interface hidden. Then I want to set up the “urlFor” methods. That will handle the largest portion of useful features in the ActionPack.

(2006-10-10 09:14:04.0) Permalink Trackback Comments [0]


Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: On

Please answer this simple math question

7 + 49 =