Wednesday, January 16, 2008

My actions GET POSTs

Quick note on a pattern I've picked up from tinkering with Rails and have happily used in other web platforms - using the same controller action for both GETs and POSTs. Basic pattern looks like this (pseudo code):


someAction()
{
if( request.isPost() ){
handle the posted data...
}
else{
handle setting up the form...
}
}


In your HTML, you don't have to specify an action, it will just POST to whatever GET was used to fetch the form in the first place. I'm implementing some file-import functionality today, and this came in handy as I can reuse the file form view for all of my various import actions.

In Rails, you can accomplish this with routing - same rout pattern sent to different actions based on the request method. There isn't an equivalent in my Struts 1.x app, so far that hasn't bothered me, though.

I would love to hear what other people think about this pattern. My initial thought was that coupling generating the form and handling the response was a negative, but in my day to day work I've yet to run into an example where coupling them together was a bad thing.