You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mississippi John Hurt <jo...@gmail.com> on 2006/07/18 21:59:13 UTC

ActionForm Use It to Populate JSP fields?

Hi,
I know ActionForm can be used to transfer html-form-input-fields to the
Action class. But what about the other way around? Can it be used to say get
a DAO object and transfer its properties to the ActionForm, which when the
request is forwarded to a jsp, is readily available or populates the labels
on the screen.  Or would I just bypass struts and just pass a POJO bean in
request to jsp which simply outputs properties as needed?  Sometimes I just
prefer to bypass struts but how's this done in Struts framework? Thanks.

Re: ActionForm Use It to Populate JSP fields?

Posted by Michael Jouravlev <jm...@gmail.com>.
If your page is read-only, then you are a lucky bastard ;-) and can
use whatever you want. Strictly speaking, you you do not have to use
ActionForm at all. I personally believe that the only value of
ActionForm class is when it is combined with <html:form> tag and you
build data entry form. This is why?

Say, you use two actions (render action and input action) and one
actionform to build a data entry form. Say you have MyForm formbean,
and it contains nested business object like Customer with properties
name and address.

Your render action displays a JSP page with <html:form> pointing to
submit action. When JSP page is rendered, Struts checks what
actionform corresponds to an action mapping referred in "action"
attribute of <html:form>. This is the same formbean that you have
prepared in your render action. Then you have say <html:text
property="name" /> and <html:text property="address" />. Struts does
the following:

* it generate HTML tags for your properties: <input type="text"
name="..." value="..."/>
* it locates a proper actionform, locates properties in it and writes
out their values to "value" attributes.
* it builds a so-called keypath for your properties, like
"MyForm.customer.name" and "MyForm.customer.address" and writes them
to "name" attributes.

Whew, render phase has finished.

Now when you submit this form, it is sent to your submit action, which
uses what a coincedence! the same actionform, MyForm. Struts processes
the request parameters that have names "MyForm.customer.name" and
"MyForm.customer.address", parses these names and sticks then into a
real Java object, which is the same Customer object nested in MyForm
actionform.

Voila, you did not have to do anything for this I/O process. This is
why having the same actionform for both render and input actions is
important. You can use one action as well if you can distinguish
between phases, usually POST for input and GET for render.

This lengthy explanation serve simply as a reasoning for actionform
usage, it works best for data entry form use case. If you have
read-only form or input-only service that does not redisplay the same
page, then you don't really need actionform. Actionform is just a Java
object that is stored in an appropriate scope by Struts for you. You
can do the same with your own beans. Ultimately all goes to servlet
container and it treats Struts actionforms or regular Java beans
equally.

Hope this helps ;-)

On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> But what if my jsp page that I want to display the DAO info is a read only
> page ie. doesn't have an <html:form> element on it?  Would I still populate
> the ActionForm and use it to display or would I just pass a regular POJO to
> jsp and use <bean:write> or jsp or scriptlet to display?
>
> On 7/18/06, Michael Jouravlev <jm...@gmail.com> wrote:
> >
> > On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> > > Hi,
> > > I know ActionForm can be used to transfer html-form-input-fields to the
> > > Action class. But what about the other way around? Can it be used to say
> > get
> > > a DAO object and transfer its properties to the ActionForm, which when
> > the
> > > request is forwarded to a jsp, is readily available or populates the
> > labels
> > > on the screen.
> >
> > Of course. See this for some insights:
> > http://wiki.apache.org/struts/StrutsManualActionClasses You can use
> > one action or two actions and just one actionform. The actionform acts
> > as I/O buffer.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionForm Use It to Populate JSP fields?

Posted by Hubert Rabago <hr...@gmail.com>.
That's how I do it most of the time.  If your POJO is in session
scope, your Action won't even have to do anything.

I usually only use ActionForm when there's a corresponding HTML
<form>.  If I'm just displaying data on the page, I just pass the
POJO.

Hubert


On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> But what if my jsp page that I want to display the DAO info is a read only
> page ie. doesn't have an <html:form> element on it?  Would I still populate
> the ActionForm and use it to display or would I just pass a regular POJO to
> jsp and use <bean:write> or jsp or scriptlet to display?
>
> On 7/18/06, Michael Jouravlev <jm...@gmail.com> wrote:
> >
> > On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> > > Hi,
> > > I know ActionForm can be used to transfer html-form-input-fields to the
> > > Action class. But what about the other way around? Can it be used to say
> > get
> > > a DAO object and transfer its properties to the ActionForm, which when
> > the
> > > request is forwarded to a jsp, is readily available or populates the
> > labels
> > > on the screen.
> >
> > Of course. See this for some insights:
> > http://wiki.apache.org/struts/StrutsManualActionClasses You can use
> > one action or two actions and just one actionform. The actionform acts
> > as I/O buffer.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionForm Use It to Populate JSP fields?

Posted by Mississippi John Hurt <jo...@gmail.com>.
But what if my jsp page that I want to display the DAO info is a read only
page ie. doesn't have an <html:form> element on it?  Would I still populate
the ActionForm and use it to display or would I just pass a regular POJO to
jsp and use <bean:write> or jsp or scriptlet to display?

On 7/18/06, Michael Jouravlev <jm...@gmail.com> wrote:
>
> On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> > Hi,
> > I know ActionForm can be used to transfer html-form-input-fields to the
> > Action class. But what about the other way around? Can it be used to say
> get
> > a DAO object and transfer its properties to the ActionForm, which when
> the
> > request is forwarded to a jsp, is readily available or populates the
> labels
> > on the screen.
>
> Of course. See this for some insights:
> http://wiki.apache.org/struts/StrutsManualActionClasses You can use
> one action or two actions and just one actionform. The actionform acts
> as I/O buffer.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: ActionForm Use It to Populate JSP fields?

Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/18/06, Mississippi John Hurt <jo...@gmail.com> wrote:
> Hi,
> I know ActionForm can be used to transfer html-form-input-fields to the
> Action class. But what about the other way around? Can it be used to say get
> a DAO object and transfer its properties to the ActionForm, which when the
> request is forwarded to a jsp, is readily available or populates the labels
> on the screen.

Of course. See this for some insights:
http://wiki.apache.org/struts/StrutsManualActionClasses You can use
one action or two actions and just one actionform. The actionform acts
as I/O buffer.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org