Sprout - annotation powered simplicity for Struts

来源:百度文库 编辑:神马文学网 时间:2024/04/28 03:51:19
Links
Introduction
Motivation
Download Sprout 0.9.1
API Documentation
Sprout
Annotation-PoweredSimplicity for Struts
2/19/06:Sprout 0.9.1 released.
Overview
Sprout aims to significantly simplify development with Struts byreducing the amount of configuration required through the use ofannotations.
Basics
Sprout requires JDK 1.5 and Struts 1.2.6 or later. Without those, you‘re out of luck.
Sprout is an extension of a StrutsMappingDispatchAction, which allows for multiple actions tobe defined within the same Action class. In this case, thename of the method is mapped to the URL (after converting method names;methodName is exposed as /method_name.do). Paths aredetermined based on the package name of the action. For example,net.mojodna.sprout.action.HomeAction corresponds to /,while net.mojodna.sprout.action.example.ExampleActioncorresponds to /example/.
Sprout also uses a custom RequestProcessor —SproutRequestProcessor, which extends Spring‘sDelegatingRequestProcessor. This means that you can specifydependencies within your actions using setter-injection.
Sprout is also completely backward-compatible with legacyStruts applications.. It was built for use in a legacy Strutsapplication; many of the older Actions are untouched — newdevelopment is done using Sprout (I often find myself removingaction-mappings while adding new functionality).
Annotations
All annotations are optional.
@FormName
Allows the developer to override the name of the form-bean (definedin struts-config.xml) used for this method. This is equivalentto setting the name attribute within an actionmapping.
Defaults to ${action-name}Form; e.g. forAdminAction the default ActionForm name would beAdminActionForm.
@Forward
Specifies additional forwards. Multiple forwards may be specified byproviding arrays as arguments to name, path, andredirect. redirect defaults to false.
A default redirect is provided; the key isSprout.FWD_SUCCESS and the path is the converted path +.jsp. E.g., AdminAction.methodName() corresponds tomethod_name.jsp.
e.g. @Forward(name="failure", path="/failure.jsp"redirect="true")
@Input
This annotation is required if this action is validating the outputfrom a different action. In that case, the argument to @Inputshould be the path to the JSP containing the form whose input is beingvalidated.
e.g @Input("login.jsp") if this is not login()and the action that initiated this request islogin().
@Scope
Specifies the scope attribute for the generated actionmapping. This exists primarily for completeness; it is likely that youmay never use this annotation.
As with struts-config.xml, the default isrequest.
@Validate
Specifies the validate attribute for the generated actionmapping. Set this to true if you desire the output of thisaction to be validated. For this to have any effect, you must havespecified rules in validator-rules.xml.
Sprout does not contain anything to ease the actual validationprocess at this time.
Example
src/java/net/mojodna/sprout/action/example/ExampleAction.java:
// URL should be /example/*package net.mojodna.sprout.action.example;
// ...
public class ExampleAction extends Sprout {// overrides Sprout.index()public ActionForward index( ... ) {// do something
// redirect to index.jspreturn mapping.findForward( FWD_SUCCESS );}}
_src/java/applicationContext.xml:
......
src/web/WEB-INF/struts-config.xml:
......


...Shorthand
Index Actions
Sprout contains an index() method to speed up the process ofgetting something working. To use this, subclass Sprout (no methodsnecessary), register the action-bean in applicationContext.xmland create a corresponding index.jsp. When you need to addlogic to the action, override index() in your Sprout sub-classand add it there.
DynaActionForms
Helper methods have been added to ease development usingDynaActionForms.
String key = "foo";String value = "bar";// returns a Stringf( key ) == ((DynaActionForm) form).getString( key );
// returns an ObjectF( key ) == ((DynaActionForm) form).get( key );
// sets a values( key, value ) == ((DynaActionForm) form).set( key, value );ActionMessage handling
Sprout contains adaptations to the traditional way Struts handlesActionMessages. getMessages() andgetErrors() have been modified to store and retrievemessages from the session rather than the request. This means thatmessages and errors will be displayed (and subsequently cleared) on thenext invocation of or a variant(such as ), regardless of whethereither of the get methods have been called or if the invocationoccurs during a separate request.
Sample message / error handling code (within an Action):
ActionMessages msgs = getMessages( request );ActionMessages errors = getErrors( request );
// Add a messagemsgs.add( ... );
// Add an errorerrors.add( ... );
// Save messages and errorssaveMessages( request, msgs );saveErrors( request, errors );
(src/web/WEB-INF/tags/ui/notifications.tag) is an alternativetag file that can be modified for your use. The primary differencebetween is that it will displayboth messages and errors (and will discriminate between them, allowingyou to style them differently depending on your application‘sneeds).
Servlets and Taglibs
Spring‘s Struts integration lacks the ability to autowire servletsand taglibs. Sprout contains classes (Sproutlet andSproutTag) that can be subclassed to provide auto-wiringcapability. They will be wired during their initialization process.
These support classes only support the byNameauto-wiring mechanism.
Q + A
Q: Why the Spring dependencies?
A: I‘m already using Spring. There‘s a good chance that you are as well. Removing the dependency makes reflection upon the registered actions (or finding them in the first place) significantly more difficult.