You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ojay78 <oj...@gmx.de> on 2005/09/22 19:22:00 UTC

The best way to prepopulate a ActionForm

Hi @ all,

 

I stuck on a problem. I don't now how to prepoulate a Actionform. What I'm
doing is this: 

 

On my showFVKNAdminAction I receive a value from the previous ActionForm and
put it in the request so that the next handleFVKNAction have this value.

I also put a attribute in the request so that the handleFVKNAction knows
that it has to act like a "show" action not like a "update" action. Then I
get the my values that are relevant for me out of the database. I put this
List into my Viewmodel and the handleFVKNForm. Then I forward to the jsp. 

 

In the JSP I get my Viewmodell and show my values on my page with the
logic:iterate tag

<bean:define      id="fvknList"

                        name="FVKN"

                        property="fvknList"

                        scope="session"

                        type="java.util.List"/>

 

 

 

 

html:form action="/handleFVKNAction" method="post" >

            <logic:iterate name="fvknList" id="line" indexId="lineNo"
type="FVKN">

                  

                  <tr>

                        <td><%=lineNo.intValue()+1%></td>

                        <td><bean:write name="line" property="fieldName"
filter="true" /></td>

..

..

..

 

This works so far. 

 

But when I hit the submit button the handleFVKNForm is empty. Why is that
so? 

What I want is to get my List from the database, display it on the jsp page
and if the submit button will be pressed it should update this list in my
database.

I think my failure is, that this fvknList is from my Viewmodel but I should
get it from my prepopulated form bean, right? But obviously I don't know how
to do that

 

Any suggestions for a better way to solve this problem?

 




Re: The best way to prepopulate a ActionForm

Posted by Rafael Nami <ra...@gmail.com>.
Sorry ojay, my codes are in the notebook, and right now I don't have
access to the internet with it(I don't have the ppoe program to run
this ADSL connection in my note). I can send you the code from my work
tomorrow.

Best Regards
Rafael Mauricio Nami

2005/9/22, Dave Newton <ne...@pingsite.com>:
> Frank W. Zammetti wrote:
>
> >Ok, got it working.  Ugh, took me 20 minutes to find a stupid typo in the
> >blank Struts app I use to start a new project.  ARGH!
> >
> >
> Hey, does that mean you can bill clients for an extra 20 minutes for
> every project?
>
> That looks almost *exactly* like what I implemented although I suspect
> yours is substantially more robust.
>
> *sigh*
>
> Now I just use Spring. Except for my Struts-based CRUD that still uses
> its own config file. re-*sigh*
>
> Dave
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


--
Java - Assim no Server como no Palm
<i>Java - thus in the Server as it is in Palm

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


Re: The best way to prepopulate a ActionForm

Posted by Dave Newton <ne...@pingsite.com>.
Frank W. Zammetti wrote:

>Ok, got it working.  Ugh, took me 20 minutes to find a stupid typo in the
>blank Struts app I use to start a new project.  ARGH!
>  
>
Hey, does that mean you can bill clients for an extra 20 minutes for 
every project?

That looks almost *exactly* like what I implemented although I suspect 
yours is substantially more robust.

*sigh*

Now I just use Spring. Except for my Struts-based CRUD that still uses 
its own config file. re-*sigh*

Dave



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


Re: The best way to prepopulate a ActionForm

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
On Thu, September 22, 2005 3:50 pm, Frank W. Zammetti said:
> P.S. - I just threw together a quick little sample app to see if the
> filter can be used this way in the first place... it didn't work right off
> the bat, although I think it's just a mapping issue, not so much a flaw
> with the underlying concept.  I'll reply later when I have it working, in
> case anyone is interested.

Ok, got it working.  Ugh, took me 20 minutes to find a stupid typo in the
blank Struts app I use to start a new project.  ARGH!

Anyway... like I said, it works, and it's pretty simple.  In case anyone
is interested, here's all I did...

(1) I started with a simple, typical blank Struts webapp...
struts-config.xml is bare-bones:

  ...
  <form-beans>
    <form-bean name="testForm1" type="app.blank.TestForm" />
  </form-beans>
  <action-mappings>
    <action path="/test" type="app.blank.TestAction" name="testForm1"
scope="session" validate="false" input="index.jsp">
      <forward name="defaultForward" path="/test.jsp" />
    </action>
  </action-mappings>
  ...

This app contains index.jsp, which has nothing but a link pointing to
TestAction... TestAction does nothing except returning defaultForward,
which goes to test.jsp, which is simply:

  <%@ page language="java" %>
  <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
  <html>
  <head>
  <title>test.jsp</title>
  </head>
  <body>
  <html:form action="test">
    <html:text property="field1" />
  </html:form>
  </body>
  </html>

So, to that app I just dropped javawebparts-filter.jar into WEB-INF/lib. 
This depends on Commans-Beanutils, Digester and Logging, so they are
already available to your app in some fashion.

(2) Add the filter description to web.xml.  I did this:

  <filter>
    <filter-name>DependencyFilter</filter-name>
    <filter-class>javawebparts.filter.DependencyFilter</filter-class>
    <init-param>
      <param-name>configFile</param-name>
      <param-value>/WEB-INF/dependencies_config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>createSession</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>DependencyFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

createSession determines whether a session will be created by this filter
firing or not.  I think in most cases you would want it to, unless you
need to do something around session creation and a listener isn't
sufficient... Of course, if set it to false then no dependencies will be
added to session scope :)

(3) Add the dependencies_config.xml which in this case is very simple:

  <dependencies>
    <dependency scope="session" name="testForm1">
      <className>app.blank.TestForm</className>
      <initProp name="field1" value="Hello from DependencyFilter!" />
    </dependency>
  </dependencies>

field1 is a String, and it's the only thing in the ActionForm.

That's it!  Firing up the app and clicking the link results in "Hello from
DependencyFilter!" showing up in the HTML for on test.jsp, as expected.

As I mentioned before, you can do quiet a bit with DependencyFilter beyond
this... you can initialize Lists, Maps and Arrays, call methods on it or
pass it off to another named class after its fields have been populated
(if applicable) to do whatever more advanced configuration you need (i.e.,
reading from a database, etc).

One thing I did forget is that unlike most of the other filters in JWP,
this one *does not* support the more extended mapping capabilities, so you
can only configure it like a typical filter.  This is something I intend
to rectify, but I may do so by putting the path mapping info on the
<dependency> elements above, so that you can have very fine-grained
control over what paths trigger what dependencies being created.

In any case, I hope that helps someone somewhere along the way :)

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


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


Re: The best way to prepopulate a ActionForm

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
On Thu, September 22, 2005 3:31 pm, Dave Newton said:
> If you're going to put an ActionForm into session mightn't it make more
> sense to put this in a request processor, as long as it's
> Struts-dependent anyway? I dunno.

It's certainly a reasonable argument to make. :)

My feeling these days though is that anything I can do in a more "generic"
J2EE way, i.e., not Struts-specific, is good.  If I was using 1.3 I might
soften that stance a bit because it's inherently designed for extension,
and something like this would probably be at the top of many peoples'
lists.

Putting that all aside though, today I'm not using 1.3, and I work in a
shop where modifying Struts is frowned upon, so I might not even have that
option available even if I wanted to go that way.  I'm not even sure I'd
be allowed to do it with 1.3!  But at least there I can make a stronger
argument that Struts isn't really being "modified" per se.

> I implemented (more or less... tending towards less ;) this because it
> seemed like most of my apps wanted to always have this or that object in
> various scopes. It wasn't totally config-file driven but a combination
> of config file which basically supplied factory class names that would
> then do what I wanted.

If you really do always want these objects in existance in a given scope
all the time, and would prefer to not have to write the code to ensure
that, then this filter I think is a good answer to that challenge.  I
don't claim it's on the same level as Spring or anything like that, but I
think it does the job pretty nicely. :)

> This solution generally worked for me, but I sometimes found the
> delocalization a bit magical: "Man, I'm looking at the code for this
> action and I don't see ANYTHING relating to what I'm seeing in Real Life."

I have historically agreed with that viewpoint too.  I've always had a bit
of a problem looking at a piece of code and having to wonder "hey, where
do A, B and C come from?!?".  But, as with all layers of abstraction and
move towards centralization, you gain things, theoretically at least, that
make up for that.  Having one config file to go to to see how objects are
put together, not having to have the plumbing code in your classes to get
their dependencies, these are just two of the benefits to any IoC
implementation.  Heck, it's the same arguments we made for and against
JNDI years ago really :)

> *poof*

Why did I just have a vision of Merlin turning Bugs into a bat?!?

> Dave

Frank

P.S. - I just threw together a quick little sample app to see if the
filter can be used this way in the first place... it didn't work right off
the bat, although I think it's just a mapping issue, not so much a flaw
with the underlying concept.  I'll reply later when I have it working, in
case anyone is interested.


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


Re: The best way to prepopulate a ActionForm

Posted by Dave Newton <ne...@pingsite.com>.
Frank W. Zammetti wrote:

>Since an ActionForm is no different from any other class as far as the
>filter would be concerned, it can instantiate it and place it in the
>appropriate scope, which would really only mean session, since I'm pretty
>sure Struts would overwrite it if it was request-scoped... as long as you
>name it the same way Struts would name it, it should work, Struts should
>find it in session and everything would work as usual.
>  
>
If you're going to put an ActionForm into session mightn't it make more 
sense to put this in a request processor, as long as it's 
Struts-dependent anyway? I dunno.

>The benefit is that you can do all sorts of initialization on it in a
>declarative fashion, including calling certain methods on it, or passing
>it off to some other object to configure it for you.  It's what I call a
>"hybrid IoC" implementation... the dependencies are injected into either
>request or session, not directly into a client object.  Different, but
>still quite useful.
>  
>
I implemented (more or less... tending towards less ;) this because it 
seemed like most of my apps wanted to always have this or that object in 
various scopes. It wasn't totally config-file driven but a combination 
of config file which basically supplied factory class names that would 
then do what I wanted.

This solution generally worked for me, but I sometimes found the 
delocalization a bit magical: "Man, I'm looking at the code for this 
action and I don't see ANYTHING relating to what I'm seeing in Real Life."

*poof*

Dave



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


Re: The best way to prepopulate a ActionForm

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Another potentially interesting possibility, although one I have to admit
I have not tried yet, is using the DependencyFilter in Java Web Parts.

Since an ActionForm is no different from any other class as far as the
filter would be concerned, it can instantiate it and place it in the
appropriate scope, which would really only mean session, since I'm pretty
sure Struts would overwrite it if it was request-scoped... as long as you
name it the same way Struts would name it, it should work, Struts should
find it in session and everything would work as usual.

The benefit is that you can do all sorts of initialization on it in a
declarative fashion, including calling certain methods on it, or passing
it off to some other object to configure it for you.  It's what I call a
"hybrid IoC" implementation... the dependencies are injected into either
request or session, not directly into a client object.  Different, but
still quite useful.

The only problem I can think of off the top of my head is that the filter,
while it can be configured a little more extensively than a standard
filter in terms of what paths cause it to fire, it still is an
all-or-nothing proposition, i.e., whatever objects are configured for it
to create will ALL be created for every path it is configured to fire on. 
Hmm, might be a nice enhahcement to make the dependencies have a path
attribute, but I digress...

Just a though anyway.  Might work, might not, I have to try it myself!

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

On Thu, September 22, 2005 2:39 pm, Wendy Smoak said:
> From: "ojay78" <oj...@gmx.de>
>
>> Could you give a example Code for that?
>
> You can see an example of form pre-population in the code for the
> MailReader
> example app:
>
> http://svn.apache.org/viewcvs.cgi/struts/apps/trunk/shared/src/java/org/apache/struts/apps/mailreader/actions/EditSubscriptionAction.java?rev=149010&view=markup
>
> This line:
>     PropertyUtils.copyProperties(subform, subscription);
> is what copies the values from the 'subscription' object to the form bean.
>
> --
> Wendy Smoak
>
>
> ---------------------------------------------------------------------
> 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: The best way to prepopulate a ActionForm

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "ojay78" <oj...@gmx.de>

> Could you give a example Code for that?

You can see an example of form pre-population in the code for the MailReader 
example app:

http://svn.apache.org/viewcvs.cgi/struts/apps/trunk/shared/src/java/org/apache/struts/apps/mailreader/actions/EditSubscriptionAction.java?rev=149010&view=markup

This line:
    PropertyUtils.copyProperties(subform, subscription);
is what copies the values from the 'subscription' object to the form bean.

-- 
Wendy Smoak 


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


AW: The best way to prepopulate a ActionForm

Posted by ojay78 <oj...@gmx.de>.
Hi Rafael,

"I usually prepopullate an ActionForm from the Action. Like, if I have
to save a Person object, I prepopullate the ActionForm in the edit()
method, setting the ActionForm again in the web context(request or
session). And then, return the according ActionForward."

Could you give a example Code for that? 

-----Ursprüngliche Nachricht-----
Von: Rafael Nami [mailto:rafanami@gmail.com] 
Gesendet: Donnerstag, 22. September 2005 19:36
An: Struts Users Mailing List
Betreff: Re: The best way to prepopulate a ActionForm


Best Regards

Rafael Mauricio Nami

2005/9/22, ojay78 <oj...@gmx.de>:
>
>
>
> Hi @ all,
>
>
>
> I stuck on a problem. I don't now how to prepoulate a Actionform. What I'm
> doing is this:
>
>
>
> On my showFVKNAdminAction I receive a value from the previous ActionForm
and
> put it in the request so that the next handleFVKNAction have this value.
>
> I also put a attribute in the request so that the handleFVKNAction knows
> that it has to act like a "show" action not like a "update" action. Then I
> get the my values that are relevant for me out of the database. I put this
> List into my Viewmodel and the handleFVKNForm. Then I forward to the jsp.
>
>
>
> In the JSP I get my Viewmodell and show my values on my page with the
> logic:iterate tag
>
> <bean:define      id="fvknList"
>
>                         name="FVKN"
>
>                         property="fvknList"
>
>                         scope="session"
>
>                         type="java.util.List"/>
>
>
>
>
>
>
>
>
>
> html:form action="/handleFVKNAction" method="post" >
>
>             <logic:iterate name="fvknList" id="line" indexId="lineNo"
> type="FVKN">
>
>
>
>                   <tr>
>
>                         <td><%=lineNo.intValue()+1%></td>
>
>                         <td><bean:write name="line" property="fieldName"
> filter="true" /></td>
>
> ..
>
> ..
>
> ..
>
>
>
> This works so far.
>
>
>
> But when I hit the submit button the handleFVKNForm is empty. Why is that
> so?
>
> What I want is to get my List from the database, display it on the jsp
page
> and if the submit button will be pressed it should update this list in my
> database.
>
> I think my failure is, that this fvknList is from my Viewmodel but I
should
> get it from my prepopulated form bean, right? But obviously I don't know
how
> to do that
>
>
>
> Any suggestions for a better way to solve this problem?
>
>
>
>


--
Java - Assim no Server como no Palm
<i>Java - thus in the Server as it is in Palm

---------------------------------------------------------------------
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: The best way to prepopulate a ActionForm

Posted by Rafael Nami <ra...@gmail.com>.
I usually prepopullate an ActionForm from the Action. Like, if I have
to save a Person object, I prepopullate the ActionForm in the edit()
method, setting the ActionForm again in the web context(request or
session). And then, return the according ActionForward.

Best Regards

Rafael Mauricio Nami

2005/9/22, ojay78 <oj...@gmx.de>:
>
>
>
> Hi @ all,
>
>
>
> I stuck on a problem. I don't now how to prepoulate a Actionform. What I'm
> doing is this:
>
>
>
> On my showFVKNAdminAction I receive a value from the previous ActionForm and
> put it in the request so that the next handleFVKNAction have this value.
>
> I also put a attribute in the request so that the handleFVKNAction knows
> that it has to act like a "show" action not like a "update" action. Then I
> get the my values that are relevant for me out of the database. I put this
> List into my Viewmodel and the handleFVKNForm. Then I forward to the jsp.
>
>
>
> In the JSP I get my Viewmodell and show my values on my page with the
> logic:iterate tag
>
> <bean:define      id="fvknList"
>
>                         name="FVKN"
>
>                         property="fvknList"
>
>                         scope="session"
>
>                         type="java.util.List"/>
>
>
>
>
>
>
>
>
>
> html:form action="/handleFVKNAction" method="post" >
>
>             <logic:iterate name="fvknList" id="line" indexId="lineNo"
> type="FVKN">
>
>
>
>                   <tr>
>
>                         <td><%=lineNo.intValue()+1%></td>
>
>                         <td><bean:write name="line" property="fieldName"
> filter="true" /></td>
>
> ..
>
> ..
>
> ..
>
>
>
> This works so far.
>
>
>
> But when I hit the submit button the handleFVKNForm is empty. Why is that
> so?
>
> What I want is to get my List from the database, display it on the jsp page
> and if the submit button will be pressed it should update this list in my
> database.
>
> I think my failure is, that this fvknList is from my Viewmodel but I should
> get it from my prepopulated form bean, right? But obviously I don't know how
> to do that
>
>
>
> Any suggestions for a better way to solve this problem?
>
>
>
>


--
Java - Assim no Server como no Palm
<i>Java - thus in the Server as it is in Palm

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


Re: The best way to prepopulate a ActionForm

Posted by Rick Reumann <st...@reumann.net>.
ojay78 wrote the following on 9/22/2005 1:22 PM:

> On my showFVKNAdminAction I receive a value from the previous ActionForm 
> and put it in the request so that the next handleFVKNAction have this value.
> 
> I also put a attribute in the request so that the handleFVKNAction knows 
> that it has to act like a “show” action not like a “update” action. Then 
> I get the my values that are relevant for me out of the database. I put 
> this List into my Viewmodel and the handleFVKNForm. Then I forward to 
> the jsp.

If it's just a show action, I wouldn't put the List into the form. Since 
you only need this list for display purposes put it into the Request.

> In the JSP I get my Viewmodell and show my values on my page with the 
> logic:iterate tag
> 
> <bean:define      id="fvknList"   ...<snip>

You shouldn't need to use bean:define at all. The list is put in request 
scope in your action before you get to the JSP, so you don't need the above.

> html:form action="/handleFVKNAction" method="post" >
> 
>             <logic:iterate name="fvknList" id="line" indexId="lineNo" 
> type="FVKN">
> 
>                  
> 
>                   <tr>
> 
>                         <td><%=lineNo.intValue()+1%></td>
> 
>                         <td><bean:write name="line" 
> property="fieldName"  filter="true" /></td>

> This works so far.

> But when I hit the submit button the handleFVKNForm is empty. Why is 
> that so?
> 
> What I want is to get my List from the database, display it on the jsp 
> page and if the submit button will be pressed it should update this list 
> in my database.

I'm not sure exactly what you mean by the above. If you do want the 
whole List submited when you submit the form, then yes you will need to 
  put the List into the form bean as a property. In that case, I'm not 
sure what exactly you are tyring to capture and update? You only showed 
<bean:write name="line" property="fieldName"  filter="true" /> which is 
display only. Are you wanting to update all the fields in the table (the 
  List) and then have that updated list available when you submit? If so 
this can be done, but I'm just not sure that's even what you are trying 
to do.

> I think my failure is, that this fvknList is from my Viewmodel but I 
> should get it from my prepopulated form bean, right? But obviously I 
> don’t know how to do that

You'd have your formBean have a property:   List fvknList; with set/get 
methods. You'll run into a few quirks when you try to submit the form if 
  the form doesn't have session scope. We can address those later when 
you want the form with request scope, but for now to get it working, 
give the form session scope and add the List as a property of your form 
bean.

-- 
Rick

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