aspose file tools*
The moose likes Struts and the fly likes Action Form Bean: Sharing beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action Form Bean: Sharing beans" Watch "Action Form Bean: Sharing beans" New topic
Author

Action Form Bean: Sharing beans

Arch Tiwari
Greenhorn

Joined: Feb 07, 2003
Posts: 6
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

Joined: May 17, 2002
Posts: 10
try to put the scope as session in the struts-config.xml it may help you.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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!


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Amit Tiwary
Greenhorn

Joined: Feb 13, 2003
Posts: 22
u
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Action Form Bean: Sharing beans
 
Similar Threads
null pointer exception in struts 1.1
Hi i am trying to connect to postgresql database using struts 1.2..i have used datasource
problem between Form and Action
validate method is not being called
Cannot find bean in any scope