• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Action Form Bean: Sharing beans

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to share the same action form bean between 2 Action classes.
I find that while invoking the 2nd Action form from the 1st using a global Forward, the values in the bean are reset to its original status, as when passed to Action 1, while invoking the Action 2. This happens even though I explicilty reset certain values before forwarding to Action 2
Can anyone tell me why this is happening or where I am going wrong. -
For instance -
I have a commonActionFormBean.java
My edit.jsp invokes the Edit Action class. It however, also contains a delete button which has the following get/set methods in the bean -
private String delete = null;
// Get delete button
public String getDelete() {
return delete;
}
// Set delete button
public void setDelete(String newDelete) {
this.delete = newDelete;
}
In my Edit Action class I check --
if (eform.getDelete() != null )
{
---- do something ---
formbean.setDelete(null);
return (mapping.findforward("delete"));
}
------
"delete" is a global forward to invoke deleteAction.do.
I find that the delete button is reset to null, however, while invoking deleteAction, it is again reset to delete = "delete";
----
Secondly, if I use 2 different sets of beans, how can I pass the values of one bean to that of the 2nd through the action classes or how can I use the Action 1 bean in Action 2.
Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to put the scope as session in the struts-config.xml it may help you.
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arch Tiwari:
In my Edit Action class I check --
if (eform.getDelete() != null )
{
---- do something ---
formbean.setDelete(null);
return (mapping.findforward("delete"));
}


Even if you change your config to use session scope, there's a big risk here. Due to the multithreaded nature of the app server, your change may or may not get updated in the session thread. Perl the JLS, each thread may have its own local copy of a variable and it is then up to the JVM to determine when to sync up those variable instances. The only guarantee is that it will happen. So if your worker thread updates a local copy, then another (or even the same) gets another local copy from the session thread, they may not match. For this and other reasons, it is generally frowned upon as a practice to modify your form beans in an action.
(You might be able to trick it by synchronizing your setter, or through another kludge I'd rather not mention, but I still recommend simply avoiding the issue. KISS.)
It might work if your form name for both actions is exactly the same. It might not. Best not to chance it.
What you can do is store another value in a request attribute and pass it along. Or, if you simply forward the request along, your form bean will still be in the request scope. Or if you use session scope, you can use the name specified in the struts-config to access it from the session object in your next action. That's fine, just beware modifying your form!
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic