Saturday, March 27, 2010

Extended errors tag for Stripes

If you ever need to have two or more event methods in a single Stripes ActionBean you can copy the ErrorsTag and add the following lines in the doStartTag method:


                if (getAction().equals(mainAction)) {
                    if (getEvent() != null) {
                        Configuration config = StripesFilter.getConfiguration();
                        ActionResolver resolver = config.getActionResolver();
                        String eventName = resolver.getEventName(mainBean.getClass(), mainBean.getContext());
                        if (getEvent().equals(eventName)) {
                            errors = mainBean.getContext().getValidationErrors();
                        }
                    } else {
                        errors = mainBean.getContext().getValidationErrors();
                    }
                }

And add get/setEvent methods and add an attribute element to the TLD file and you're set to go.


will ONLY display errors when action is "path.to.actionbean" AND event is "addObject". I've used this when having two forms on the same page associated with the same ActionBean (to avoid having to write an extra ActionBean bound to another action URI). It might be kinda anti-pattern but my two forms are close related but different data is validated using the @Validate( on="addObject" ) annotation etc.  

oohhh luv that Stripes :-) 


Sunday, March 21, 2010

And the winner is Stripes

I've been working on a hobby project of mine and I needed an extremely light weight Java based web framework for the presentation part, and I chose Stripes (link: www.stripesframework.org).

And boy am I glad I found Stripes. Stripes is darn simple, has an absolute minimal learning curve (I got started in about 15 mins) and I have yet to find a thing I dislike (other than the fact it is not a component based framework, but than again, this was one of the reasons why I chose Stripes in the first place).

Let me give you an example - I hold an absolute minimal amount of data in the session (most is bound to the request as there is no knowing when a request will hit another cluster instance) but when a validation error occurs, Stripes will by default forward to the previous page. This is mighty fine if it wasnt for the fact than that page expected a specific object to be bound to that request, so most of my renderings failed. "Well - Stripes authors might have anticipated this" I thought and what do you know - they did - you can simply implement an interface (ValidationErrorHandler) and your ActionBean can do whatever before Stripes does the actual forward and I simply bound the necessary data (again) and vupti - page displayed correctly.

AND there are no crappy XML configuration files - URL bindings are set using annotations and can include parameters (which will then be set as parameter on request) and much much more (I hope :-) )