Class Sprout
One goal is to make web application development in Java more fun. Sprouts require Java 5 (1.5) due to their use of annotations. They also require a servlet engine conforming to the Servlet 2.4 and JSP 2.0 specs (Tomcat 5.x, for example) due to the use of filters, listeners, and tag files.
Sprouts obviate the need to write Struts action-mappings as they use information gleaned from the initial Sprout, class properties (name and package), as well as annotations to appropriately self-register on URLs defined by convention or specification with properties obtained the same way.
The path is determined by the package name where the components to the
right of "action" are converted into directories. Thus,
whatever.action.* will correspond to /* and
whatever.action.help.* to /help/*
The file (or action) is determined by the method name.
Thus, index() will correspond to index.do and
submitChange() to submit_change
(CamelCased method names are converted).
(NOTE: publick() maps to public.do as
public is a Java keyword.)
Form names are created from the class name appended with Form,
so TestAction would default to TestActionForm.
This behavior can be overridden using the
@FormName("AlternateForm") annotation.
Input, validate, and scope properties can be overridden
with @Input, @Validate, and @Scope
respectively.
f(key), F(key), and s(key,value)
are helper methods that manipulate DynaActionForms (if used) and obviate
the need to cast excessively. f() is the equivalent of
calling getString(), F() get(), and
s() set().
getMessages(), getErrors(),
saveMessages(), and saveErrors() have been
modified to store state in the user's session allowing them to be used more
simply and effectively. Rather than using this:
ActionMessages errors = new ActionMessages(); ... saveErrors(request, errors);You should use getErrors() to initialize the errors ActionMessages object:
ActionMessages errors = getErrors( request ); ...This way, messages and errors can be stacked up (while being kept separate) until they are displayed using the sprout:notifications taglib (see WEB-INF/tags/sprout/notifications.tag).
TODO add additional reserved words
TODO add a default ActionForm with just an "id" field (as a String) as "SproutForm"
TODO add a GlobalForward annotation with "redirect" property to add to the
list of global-forwards.
TODO add some measure of SiteMesh integration for AJAX partials
TODO add some form of ActionForwardBuilder
- Author:
- Seth Fitzsimmons
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static @interfaceOverride the default form name for this action.protected static @interfaceSpecifies a local forward.protected static @interfaceSpecifies the "input" property for this action.protected static @interfaceSpecifies the "scope" property for this action.protected static @interfaceInstruct Struts to validate the form provided to this method. -
Field Summary
FieldsFields inherited from class org.apache.struts.actions.DispatchAction
clazz, methods, typesFields inherited from class org.apache.struts.actions.BaseAction
messagesFields inherited from class org.apache.struts.action.Action
servlet -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddErrors(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Add errors to the session.protected voidaddMessages(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Add messages to the session.protected StringShortcut for ((DynaActionForm) form).getString(key).protected ObjectShortcut for ((DynaActionForm) form).get(key).protected org.apache.struts.action.ActionMessagesgetErrors(javax.servlet.http.HttpServletRequest request) Gets undisplayed errors from both the request and the session.protected org.apache.struts.action.ActionMessagesgetMessages(javax.servlet.http.HttpServletRequest request) Gets undisplayed messages from both the request and the session.org.apache.struts.action.ActionForwardindex(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Helper method to display index.jsp in response to a request for /index.dofinal voidinit(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) protected voidonInit(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Callback for subclass-specific initialization.protected voidShortcut for ((DynaActionForm) form).set(key, value).protected voidsaveErrors(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Saves errors to the session scope so that they may be picked up by the next action that accesses errors.protected voidsaveMessages(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Saves messages to the session scope so that they may be picked up by the next action that accesses messages.Methods inherited from class org.apache.struts.actions.MappingDispatchAction
execute, getMethodName, getParameter, unspecifiedMethods inherited from class org.apache.struts.actions.DispatchAction
cancelled, dispatchMethod, getMethodMethods inherited from class org.apache.struts.action.Action
execute, generateToken, getLocale, getResources, getResources, getServlet, isCancelled, isTokenValid, isTokenValid, resetToken, saveErrors, saveMessages, saveToken, setLocale, setServlet
-
Field Details
-
SPROUT_DEFAULT_ACTION_FORM_NAME
- See Also:
-
FWD_SUCCESS
Default forward key.- See Also:
-
-
Constructor Details
-
Sprout
public Sprout()
-
-
Method Details
-
init
public final void init(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) -
onInit
protected void onInit(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Callback for subclass-specific initialization. -
f
Shortcut for ((DynaActionForm) form).getString(key). -
F
Shortcut for ((DynaActionForm) form).get(key). -
s
Shortcut for ((DynaActionForm) form).set(key, value). -
addErrors
protected void addErrors(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Add errors to the session.- Overrides:
addErrorsin classorg.apache.struts.action.Action
-
getErrors
protected org.apache.struts.action.ActionMessages getErrors(javax.servlet.http.HttpServletRequest request) Gets undisplayed errors from both the request and the session.- Overrides:
getErrorsin classorg.apache.struts.action.Action
-
saveErrors
protected void saveErrors(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Saves errors to the session scope so that they may be picked up by the next action that accesses errors.- Overrides:
saveErrorsin classorg.apache.struts.action.Action
-
addMessages
protected void addMessages(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Add messages to the session.- Overrides:
addMessagesin classorg.apache.struts.action.Action
-
getMessages
protected org.apache.struts.action.ActionMessages getMessages(javax.servlet.http.HttpServletRequest request) Gets undisplayed messages from both the request and the session.- Overrides:
getMessagesin classorg.apache.struts.action.Action
-
saveMessages
protected void saveMessages(javax.servlet.http.HttpServletRequest request, org.apache.struts.action.ActionMessages msgs) Saves messages to the session scope so that they may be picked up by the next action that accesses messages.- Overrides:
saveMessagesin classorg.apache.struts.action.Action
-
index
public org.apache.struts.action.ActionForward index(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException Helper method to display index.jsp in response to a request for /index.do- Throws:
IOExceptionjavax.servlet.ServletException
-