打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Sprout - annotation powered simplicity for Struts
userphoto

2007.07.22

关注

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 RequestProcessorSproutRequestProcessor, 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:

...<bean name="ExampleAction"class="net.mojodna.sprout.action.example.ExampleAction" singleton="true"/>...

src/web/WEB-INF/struts-config.xml:

...<form-bean name="ExampleActionForm"type="org.apache.struts.validator.DynaValidatorForm"><form-property name="..." type="..." />...</form-bean>
<!-- No action-mappings!!! --><action-mappings />
<!-- Define an alternate RequestProcessor --><controllerprocessorClass="net.mojodna.sprout.SproutRequestProcessor" />
<!-- Sprout plug-in --><plug-in className="net.mojodna.sprout.SproutAutoLoaderPlugIn" />...

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 <html:messages /> or a variant(such as <ui:notifications />), 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 );

<ui:notifications />(src/web/WEB-INF/tags/ui/notifications.tag) is an alternativetag file that can be modified for your use. The primary differencebetween <html:messages /> 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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
struts2最简单的配置(不信你来看看)
运用Jakarta Struts的七大实战心法
关于struts2中namespace和s:form action和form action 路径问题
Struts Hibernate简化J2EE的文件操作
java学习路线图
Struts2利用convention
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服