File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action Form Bean: Sharing beans

 
Arch Tiwari
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Vikram Anand
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to put the scope as session in the struts-config.xml it may help you.
 
David Hibbs
Ranch Hand
Posts: 374
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Amit Tiwary
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic