Struts - how to prepopulate a Form Bean with data

M

matthewmacchia

Hello,
So here is my goal.
I have a form that I want to prepopulate with data. For example, Let's
say I have a loginForm and I want it to prepopulate with information
about what page it is so that my nav can build itself.
I want the form to preopolate with information about that.
kind of like this:

<form-bean
name="logonForm"
type="com.whatever.LogonForm">
<form-property
name="navigation"
type="java.lang.String" value="loginpage" />
</form-bean>

It sounds dumb but I need it constructed with the data prepopulating.
(this is the definition for the dynactionform,but I need an element
like form-property)...

any ideas?
thanks
matt
 
W

Wendy S

It sounds dumb but I need it constructed with the data prepopulating.
(this is the definition for the dynactionform,but I need an element
like form-property)...

It doesn't sound dumb at all... it's why <form-property> has an 'initial'
attribute. :) Assuming your loginForm _is_ a dynamic form, that is. Or are
you trying to prepopulate a "regular" ActionForm?

The DTD is well commented-- it's worth a read even if you don't understand
the syntax:
http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd

For <form-property> it says:
initial String representation of the initial value for this
property.
If not specified, primitives will be initialized to
zero and
objects initialized to the zero-argument instantiation
of that
object class. For example, Strings will be initialized
to ""
 
M

matthewmacchia

Actually, yes.
It's with plain ol' Action Forms.
We need to keep our forms very stable so we can have a clear idea what
inputs are needed for each action.

It is my understanding that DynaActionForms would give us the ability
to mix and match forms more easily. Is that correct?

thanks.
-matt
 
W

Wendy S

Actually, yes.
It's with plain ol' Action Forms.
We need to keep our forms very stable so we can have a clear idea what
inputs are needed for each action.
It is my understanding that DynaActionForms would give us the ability
to mix and match forms more easily. Is that correct?

No, dynamic forms just get you out of writing the get/set methods. (I guess
you could argue that ActionForms contribute to more stable code... if you
write getName() and the name property doesn't exist, it'll fail at compile
time, where with a dynamic form, you wouldn't know that
form.getString("name") was not going to work.

Pre-populate your ActionForms in the Action before you forward to the view
[usually JSP].

Here's one way:
http://struts.apache.org/faqs/newbie.html#prepopulate

Another is to use some variant of DispatchAction that will 'know' what it
needs to do based on the presence of various things either in the request or
as part of the URL.
 
N

nooobody

Hello,
So here is my goal.
I have a form that I want to prepopulate with data. For example, Let's
say I have a loginForm and I want it to prepopulate with information
about what page it is so that my nav can build itself.
I want the form to preopolate with information about that.
kind of like this:

<form-bean
name="logonForm"
type="com.whatever.LogonForm">
<form-property
name="navigation"
type="java.lang.String" value="loginpage" />
</form-bean>

It sounds dumb but I need it constructed with the data prepopulating.
(this is the definition for the dynactionform,but I need an element
like form-property)...

any ideas?
thanks
matt


If you building a nav menu based on what page you are on, another
approach, but only if you are using tiles, is to set a property for
each page in the tile definition, retrieve it in one of the page's
jsps and build the menu accordingly. A custom tag might be handy for
building the menu.
 
M

matthewmacchia

Hmmmm...
I'm actually using tiles but have never tried using tiles definitions.
Could you tell me more about it?

That seems perfect.
Specifically, I am looking for a way to externalize the navigation
information, I have currently built it so that each action tells the
page where it. But, I would prefer to seperate that logic so the
actions aren't so tied to the view. That was why putting it into the
struts config and then prepopulating a form would work great. But it
doesn't seem possible.

Thanks
-matt
 
N

nooobody

Hmmmm...
I'm actually using tiles but have never tried using tiles definitions.
Could you tell me more about it?

That seems perfect.
Specifically, I am looking for a way to externalize the navigation
information, I have currently built it so that each action tells the
page where it. But, I would prefer to seperate that logic so the
actions aren't so tied to the view. That was why putting it into the
struts config and then prepopulating a form would work great. But it
doesn't seem possible.

The technique we use makes it unnecessary to set any values in
form-beans or actions.

what I meant by tiles definitions is to have an entry in your
struts-config for the tiles plugin something like this:

<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml"/>
</plug-in>

then in the tile-defs.xml (or whatever you choose to name it) you have
an entry for each page:

<definition name="logonpage" extends="main.layout">
<put name="title" value="This is the logon page" />
<put name="body" value="/jsp/body/logon.jsp" />
<put name="nav" value="/jsp/nav.jsp"/>
<put name="page" value="page1"/>
</definition>

Of course there would be other page definitions and a definition for
main.layout in the definitions-config file

Part of the trick is how to get at the 'page' value from your the
nav.jsp. I don't recall exactly how we did it - I know you need to get
it into page scope - we just started using this approach on this
project - but I can post it when I have a chance to look at it again,
maybe sunday or Monday, unless someone else is familiar with this
approach and can post the rest of the details.

Tim B
 
N

nooobody

Part of the trick is how to get at the 'page' value from your the
nav.jsp. I don't recall exactly how we did it - I know you need to get
it into page scope - we just started using this approach on this
project - but I can post it when I have a chance to look at it again,
maybe sunday or Monday, unless someone else is familiar with this
approach and can post the rest of the details.

Tim B

here it is.

in the jsp used for the base layout:
<tiles:useAttribute name="currentPage" scope="request" />

then in your jsp or custom struts tag, just get the "currentPage"
attribute that will now be found in the request and build your menu
acordingly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top