You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Schaub, Stephen" <st...@agilysys.com> on 2005/07/11 20:07:37 UTC
Putting execute() in ActionForm
I'm interested in some feedback on the following:
I prefer to develop web applications using a page-centric model. I like the simplicity of one JSP <-> one Java class for the view layer (not the model). Struts, with its separate Action and ActionForm classes, tends toward at least two Java classes for a JSP: an Action, and an ActionForm. This tends toward a multiplicity of Java classes. (I know it's possible to share a single ActionForm and Action for multiple JSP's, but I'm looking for a simpler approach.)
I would like to create a generic Action class that delegates request processing to a SimpleActionForm, like this:
public abstract class SimpleActionForm extends ActionForm {
public abstract ActionForward execute(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
public class SimpleAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
SimpleActionForm bean = (SimpleActionForm)form;
return bean.execute(mapping, request, response);
}
}
With this approach, most JSP pages could have a single Java class -- a SimpleActionForm subclass that handles both validation and processing for the page:
public class LoginForm extends SimpleActionForm {
private String username;
private String password;
... getter and setter methods ...
public abstract ActionForward execute(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
... Login processing ...
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
... validation code ...
}
}
Then, in my struts-config.xml file, most form beans would extend the SimpleActionForm, and most actions would use the SimpleAction type:
<form-beans>
<form-bean name="loginform" type="LoginForm" /> <!-- extends SimpleActionForm -->
</form-beans>
<action path="/login" type="SimpleAction" name="loginform" scope="request"
validate="true" input="/login.jsp">
...
</action>
I don't have a lot of experience with Struts, so I'm interested on feedback about this approach. Is it a good/bad idea? If bad, why?
Thanks in advance,
Stephen
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Putting execute() in ActionForm
Posted by Rick Reumann <st...@reumann.net>.
Schaub, Stephen wrote the following on 7/11/2005 2:07 PM:
> public class SimpleAction extends Action { public ActionForward
> execute( ActionMapping mapping, ActionForm form, HttpServletRequest
> request, HttpServletResponse response) throws Exception {
> SimpleActionForm bean = (SimpleActionForm)form; return
> bean.execute(mapping, request, response); } }
You can do that, although I still like my business logic done higher up
in the Action (Servlet) that I submit to rather than having it be done
in the Object that is backing the view (your ActionForm subclass).
Michael Jouravlev on this list does this and his stuff looks very nice
if you want to take this approach.
>
> With this approach, most JSP pages could have a single Java class --
> a SimpleActionForm subclass that handles both validation and
> processing for the page:
Well they certainly wouldn't have a 'single' java class, you would have
a LOT of them. I can have one ActionForm (if I wanted) for my entire
application but you will definitely have many of them to process the
business logic. You mean you'll have 'one' java class that is extended.
I don't see the big deal of that since Struts has the same concept..
your ActionForms extend a flavor of the ActionForm base class.
>
> public class LoginForm extends SimpleActionForm { private String
> username; private String password; ... getter and setter methods ...
>
>
> public abstract ActionForward execute( ActionMapping mapping,
> HttpServletRequest request, HttpServletResponse response) throws
> Exception { ... Login processing ... } public ActionErrors
> validate(ActionMapping mapping, HttpServletRequest req) { ...
> validation code ... }
>
> }
I don't think you meant for that to be abstract. But notice you know
have an execute in all your ActionForms? How is that different than
simply the processing done in different Actions as Struts is now? All
you've done is pushed down the processing to a different place..
ActionForm vs Action.
>
> Then, in my struts-config.xml file, most form beans would extend the
> SimpleActionForm, and most actions would use the SimpleAction type:
>
> <form-beans> <form-bean name="loginform" type="LoginForm" /> <!--
> extends SimpleActionForm --> </form-beans>
Ok, yes they'd have to extend that in order to utilize your processing
in the ActionForm functionality. Bottom line is I think all you've done
is move where you do the processing... you moved it from the Actions to
the ActionForms. If you like this approach definitely check out
StrutsDialog. (Sorry Michael, not trying to oversimplify all that your
stuff does, I realize it's much more than processing in the ActionForm,
but if he's going to do this he might as well stand on the shoulders of
someone who has done all the work already:)
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Stateful Pages (was Re: Putting execute() in ActionForm)
Posted by Craig McClanahan <cr...@gmail.com>.
On 7/14/05, Michael Jouravlev <jm...@gmail.com> wrote:
>
> You mean, to have analog of _viewstate in ASP.NET? I know that JSF
> supports it too, but in JSF state is saved on server by default, which
> is a relief. JSF backing beans have "session" scope by default, no
> hidden fields are used. Thus, you are free to leave the application,
> browse somewhere else, and then return back. And separation between
> input and output is a snap, just add <redirect> element in the
> navigation rule, and voila! The view will be loaded with a separate
> GET request, and will be rendered using data, stored on the server. I
> would switch to JSF only for that.
>
Just one minor clarification ... while the default for component state
saving is indeed server side, there is no default for backing beans
... you must explicitly declare the <managed-bean-scope> you want to
use for each such bean.
And a note on an option that JSF provides ... JSF components have, in
addition to the typical JavaBeans properties, a Map property called
"attributes" that can be used to store arbitrary data. It's quite
common for components (or applications) to store state information on
some component in the hierarchy (often the form component, or perhaps
the view root) and let JSF take care of saving and restoring it. That
way, you can keep your backing beans themselves in request scope
without losing anything, because they only contain state within the
context of the current request.
The only restriction to this is that the state info you stash has to
be Serializable.
Craig
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org
Re: Stateful Pages (was Re: Putting execute() in ActionForm)
Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/14/05, Michael Jouravlev <jm...@gmail.com> wrote:
> * It is impossible to get read of POSTDATA situation
Typo, must rid: "to get rid".
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org
Re: Stateful Pages (was Re: Putting execute() in ActionForm)
Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/14/05, Ted Husted <te...@gmail.com> wrote:
> <<Switching to dev list>>
>
> On 7/13/05, Michael Jouravlev <jm...@gmail.com> wrote:
> > On 7/13/05, Ted Husted <te...@gmail.com> wrote:
> > > Most importantly, I'd make very sure that all my "code-behind
> > > ActionForms" were request scope, probably by changing the factory
> > > default.
> >
> > In this case this form would not be stateful. At least for me, it is
> > the whole point of sticking data and behavior and state in one place.
> > Redirection does not work well with request scope. And redirection is
> > the must to cleanly separate input phase from output phase.
>
> Yes, a stateful page mitigates the need to redirect. The workflow
> becomes less reliant on URIs and more reliant on the state of the
> controls. Using techniques like hidden panels, many workflows that
> consume multiple stateless pages, can be done via one stateful page
> with multiple panels.
>
> If I wanted to add a "viewstate" to Struts Classic, then I might
> serialize the ActionForm to the page, probably by extending the
> html:form tag. It already writes one hidden field to support
> single-submits, why not have it write another to retain the original
> ActionForm? For 1.2.x, the ActionForm could be deseralized as part of
> validate, being sure to not overwrite any non-null fields. For 1.3.x,
> the catalog could be amended to deserialize the ActionForm before
> populating it from the request.
You mean, to have analog of _viewstate in ASP.NET? I know that JSF
supports it too, but in JSF state is saved on server by default, which
is a relief. JSF backing beans have "session" scope by default, no
hidden fields are used. Thus, you are free to leave the application,
browse somewhere else, and then return back. And separation between
input and output is a snap, just add <redirect> element in the
navigation rule, and voila! The view will be loaded with a separate
GET request, and will be rendered using data, stored on the server. I
would switch to JSF only for that.
Thus, JSF [in its default configuration] allows to produce more robust
and more user-friendly apps then ASP.NET and Struts [in their default
configuration].
Contrary, with _viewstate it is impossible to use two-phase request
processing. Microsoft geniuses thought that storing data in the
session puts too much load on a web farm, so they decided to spread
the load over the Net. Of course, they themselves knew different ways
to track the state, and they suggested to developers to weigh all pro
and cons and either use _viewstate, or turn it off and use session or
even persisted-to-database data to track the state. But who would
bother analysing, if most examples of ASP.NET code is based on
_viewstate? The consequences are devastating:
* tens of kilobytes of useless junk (especially if you display bunch
of rows of data) are moved around the wire
* all pages must be HTML forms to store hidden fields
* a user cannot jump out from the application, and then navigate back
using direct URL in the address bar, because there is nothing in the
session
* result page is returned immediately in response to submitted HTML
form to keep hidden fields, thus clean page reload is impossible and
results in double submit.
* It is impossible to get read of POSTDATA situation, though it can be
processed on the server.
* Overall user experience is horrid.
My friend works in a company, which uses ASP.NET for a pretty large
project with warehouse, shopping carts, etc. They do not use
_viewstate. Instead, they store data from each request in the
database. Expensive? Maybe. But it is much easier to add more database
servers in the cluster. Database clustering is very robust, and from
an application point of view, they still use one single database, so
the app structure is very simple.
Even storing data in the session is not *that* expensive. Servers are
cheap, memory is cheap. If needed, I would call Cameron to haggle over
the price for Coherence ;) Anyway, conceptually storing state on
server is better. If a problem can be solved by adding more memory,
this is not a problem, after all. Microsoft proves it with every
Windows release.
Applications are made for users. Storing state on server provides
better user experience. The choice should be clear.
Michael.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org
Stateful Pages (was Re: Putting execute() in ActionForm)
Posted by Ted Husted <te...@gmail.com>.
<<Switching to dev list>>
On 7/13/05, Michael Jouravlev <jm...@gmail.com> wrote:
> On 7/13/05, Ted Husted <te...@gmail.com> wrote:
> > Most importantly, I'd make very sure that all my "code-behind
> > ActionForms" were request scope, probably by changing the factory
> > default.
>
> In this case this form would not be stateful. At least for me, it is
> the whole point of sticking data and behavior and state in one place.
> Redirection does not work well with request scope. And redirection is
> the must to cleanly separate input phase from output phase.
Yes, a stateful page mitigates the need to redirect. The workflow
becomes less reliant on URIs and more reliant on the state of the
controls. Using techniques like hidden panels, many workflows that
consume multiple stateless pages, can be done via one stateful page
with multiple panels.
If I wanted to add a "viewstate" to Struts Classic, then I might
serialize the ActionForm to the page, probably by extending the
html:form tag. It already writes one hidden field to support
single-submits, why not have it write another to retain the original
ActionForm? For 1.2.x, the ActionForm could be deseralized as part of
validate, being sure to not overwrite any non-null fields. For 1.3.x,
the catalog could be amended to deserialize the ActionForm before
populating it from the request.
On the roadmap, this sort of thing would fall under "Consider a
"FormContext" mechanism".
I'm totally booked with C# work through November, but I'm thinking of
taking December off to work on whatever needs to be done around here,
which could include gizmos like a stateful ActionForm (if no one beats
me to it).
-Ted.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org
Re: Putting execute() in ActionForm
Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/13/05, Ted Husted <te...@gmail.com> wrote:
> Most importantly, I'd make very sure that all my "code-behind
> ActionForms" were request scope, probably by changing the factory
> default.
In this case this form would not be stateful. At least for me, it is
the whole point of sticking data and behavior and state in one place.
Redirection does not work well with request scope. And redirection is
the must to cleanly separate input phase from output phase. See, I was
right calling you 'orthodox' :))
Michael.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Putting execute() in ActionForm
Posted by Ted Husted <te...@gmail.com>.
In 7/11/05, Michael Jouravlev <jm...@gmail.com> wrote:
> I wanted to post something like "Rick, Ted and other orthodox guys,
> check out this guy's suggestion", but then I decided not to ;-)
Wow! That may have been the first time anyone has ever accused me of
being "orthodox" :)
I've never used this strategy myself, but I don't see anything wrong
with it. In fact, if I were to start coding in Java again, this is
the strategy I would probably use. At this point, I'm accustom to
having a code-behind, and the ActionForm is the closest thing Struts
has to a code-behind.
If I were to use this strategy, I'd select the parameters to the
Execute signature with care. There may not be any reason to pass the
HTTP parameters out of the call, and there may be a reason to pass
the Action. I would just be careful of using the parameters from the
Struts Execute signature, unless my code actually needed those
parameters..
I also wouldn't hestitate to put some "traffic cop" code into the base
Action as needed, so that the ActionForm execute can focus on what it
needs to do, rather than what the application needs to do.
I'd also consider using a base ActionForm with all the properties my
application needs, which I could then extend with execute and validate
methods, as needed.
Though, I'd probably use a DynaActionForm instead of a conventional
ActionForm as the base class, just for the agility.
Most importantly, I'd make very sure that all my "code-behind
ActionForms" were request scope, probably by changing the factory
default.
-Ted.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Putting execute() in ActionForm
Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/11/05, Rick Reumann <st...@reumann.net> wrote:
> Schaub, Stephen wrote the following on 7/11/2005 2:07 PM:
>
> > With this approach, most JSP pages could have a single Java class --
> > a SimpleActionForm subclass that handles both validation and
> > processing for the page:
>
> Michael, is this guy a plant from your StrutsDialog camp?:)
I wanted to post something like "Rick, Ted and other orthodox guys,
check out this guy's suggestion", but then I decided not to ;-)
Michael.
Oh, almost forgot the plug: Struts Dialogs,
http://struts.sourceforge.net/strutsdialogs/index.html
;-)
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Putting execute() in ActionForm
Posted by Rick Reumann <st...@reumann.net>.
Schaub, Stephen wrote the following on 7/11/2005 2:07 PM:
> With this approach, most JSP pages could have a single Java class --
> a SimpleActionForm subclass that handles both validation and
> processing for the page:
Michael, is this guy a plant from your StrutsDialog camp?:)
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org