You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Baldwin <mi...@sun.com> on 2001/09/27 15:17:37 UTC

Workflow Control and Action Reuse (newbie)

I just started looking at struts 4 days ago, so please  bear with me...

This question really gets into workflow management.

I have a certain action that would be reused frequently across my site.
I want to route to that action from many other actions and then return
both control and information back to the calling action.  i.e., Actions
A,B,C and D transfer control to Action x.  Once x has done its work, I
need for x to route control back to A, B, C or D appropriately and to
provide some "new" request information that was gathered during Action x
to these Actions A,B,C or D.  The problem is that actions A,B,C and D
all have different interfaces on their beans so it might be a little
challenging to have 'x' simply know which methods on bean A,B,C or D to
use. I think I might want to use a mediator to handle the interactions
between the beans for A,B,C,D and X

i.e. action flow control works like this...
A <--> x
B <--> x
C <--> x
D <--> x


It seems to me that one way to do this is to:
1. Have A, B, C or D place a Mediator-pattern object in the request
2. Have A,B,C and D forward (& redirect the request) to action x.
3. Action x would look for the Mediator object under a certain key and
would use the mediator to access information from bean A,B,C or D while
x does its work.
4. When x finishes, it would then update the mediator which would update
the original Bean used by A,B,C or D
5. Finally, x would ask the mediator for the message that would be
needed to restart the original action and would redirect the user back
there.

Has anyone else tried to solve this problem differently? similarly? more
or less happily?

thanks,
--Michael





BeanUtils.populate() not transferring values to bean

Posted by Jonathan M Crater <jo...@fanniemae.com>.
when i run the following test class with the org.apache.commons.beanutils.BeanUtils
class the output is null across the board.  when i run the same test with
org.apache.struts.util.BeanUtils all the values are transferred from the Map.
anybody have a similar issue?  anybody have an explanation?

import org.apache.commons.beanutils.BeanUtils;
import java.util.*;
import java.sql.*;
import java.text.*;

public class Populate {
    public static void main(String[] a) {
  try {
   String trackingId = "sometrackingid";
   Timestamp submitDate = new Timestamp(System.currentTimeMillis());
   String fileName = "filename";
   Timestamp lastUpdated = new Timestamp(System.currentTimeMillis());
   int status = 24;
   String contactId = "contact";
   int initialRequestCount = 4;
   int suspendedRequestCount = 6;
   int pendingRequestCount = 5;
   int processedRequestCount = 7;
   int deletedRequestCount = 10;

   SubmissionValueObject svo = new SubmissionValueObject();
   Map m = new HashMap();

   m.put("trackingId", trackingId);
   m.put("submitDate", getFormattedTimestamp(submitDate));
   m.put("fileName", fileName);
   m.put("lastUpdated", getFormattedTimestamp(lastUpdated));
   m.put("status", status + "");
   m.put("contactId", contactId);
   m.put("initialRequestCount", initialRequestCount + "");
   m.put("suspendedRequestCount", suspendedRequestCount + "");
   m.put("pendingRequestCount", pendingRequestCount + "");
   m.put("processedRequestCount", processedRequestCount + "");
   m.put("deletedRequestCount", deletedRequestCount + "");

   BeanUtils.populate(svo, m);

   print("trackingId= " + svo.getTrackingId());
   print("submitDate= " +  svo.getSubmitDate());
   print("fileName= " + svo.getFileName());
   print("lastUpdated= " + svo.getLastUpdated());
   print("status= " + svo.getStatus());
   print("contactId= " + svo.getContactId());
   print("initialRequestCount= " + svo.getInitialRequestCount());
   print("suspendedRequestCount= " + svo.getSuspendedRequestCount());
   print("pendingRequestCount= " + svo.getPendingRequestCount());
   print("processedRequestCount= " + svo.getProcessedRequestCount());
   print("deletedRequestCount= " + svo.getDeletedRequestCount());
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }

 public static final String getFormattedTimestamp(Timestamp t) {
  if(t == null)
   return null;

  java.util.Date d = (java.util.Date)t;
  SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
  return sdf.format(d);
 }

 static void print(String s) {
  System.out.println(s);
 }
}




Re: Workflow Control and Action Reuse (newbie)

Posted by Ted Husted <hu...@apache.org>.
The attribute name for an ActionForm bean is declared in the Struts
configuration (struts-config.xml).

The ActionForm beans are populated by the ActionServlet using the
BeanUtil.populate() method, which makes efficient use of reflection.

Michael Baldwin wrote:
> 
> Thanks, Ted,
> 
> If you have the time to dialog a little further...
> 
> It seems like struts automatically populates beans based on the form name.  Is
> this correct?
> If so, does the bean name have to explicitly be declared in the struts-config
> for the action or does struts simply look for the bean's existance in the
> session/request?
> 
> thanks,
> --Michael
> 
> P.S.  Read your FAQ page(s).  Very helpful.  I like the catalog.
> 
> Ted Husted wrote:
> 
> > I would suggest that if you feel a need to reuse an Action, or forward a
> > request between Actions, then there might be too much business logic
> > embedded in the Actions.
> >
> > The best use (I've found) for Actions is as simple adapters between HTTP
> > and your business logic. The only "work" an Action should do is analyze
> > the outcome of a business logic operation, and then to relay any errors
> > and route to the next view. If it is doing more work than that, it is no
> > longer in the Web tier, and your business logic could not be used in
> > another environment or easily subjected to Unit Testing.
> >
> > I would say that the actual work should be taking place in A,B,C,D
> > objects that can be called by any Action that needs them. Whatever data
> > they need can be removed from the Servlet contexts, and passed as simple
> > parameters. The Action just needs to handle any errors or flow control
> > decisions. If either decision is complex, then that is an indication
> > that another helper is needed ;-)
> >
> > Some business logic objects can be instantiated in the Action as part of
> > the request. Another approach would be to instantiate them once in the
> > application context, as Struts does with the ActionMappings and message
> > resources. Any Action could then pass a data bean back and forth as
> > needed with the client's data.
> >
> > -- Ted Husted, Husted dot Com, Fairport NY USA.
> > -- Custom Software ~ Technical Services.
> > -- Tel +1 716 737-3463
> > -- http://www.husted.com/about/struts/
> >
> > Michael Baldwin wrote:
> > >
> > > I just started looking at struts 4 days ago, so please  bear with me...
> > >
> > > This question really gets into workflow management.
> > >
> > > I have a certain action that would be reused frequently across my site.
> > > I want to route to that action from many other actions and then return
> > > both control and information back to the calling action.  i.e., Actions
> > > A,B,C and D transfer control to Action x.  Once x has done its work, I
> > > need for x to route control back to A, B, C or D appropriately and to
> > > provide some "new" request information that was gathered during Action x
> > > to these Actions A,B,C or D.  The problem is that actions A,B,C and D
> > > all have different interfaces on their beans so it might be a little
> > > challenging to have 'x' simply know which methods on bean A,B,C or D to
> > > use. I think I might want to use a mediator to handle the interactions
> > > between the beans for A,B,C,D and X
> > >
> > > i.e. action flow control works like this...
> > > A <--> x
> > > B <--> x
> > > C <--> x
> > > D <--> x
> > >
> > > It seems to me that one way to do this is to:
> > > 1. Have A, B, C or D place a Mediator-pattern object in the request
> > > 2. Have A,B,C and D forward (& redirect the request) to action x.
> > > 3. Action x would look for the Mediator object under a certain key and
> > > would use the mediator to access information from bean A,B,C or D while
> > > x does its work.
> > > 4. When x finishes, it would then update the mediator which would update
> > > the original Bean used by A,B,C or D
> > > 5. Finally, x would ask the mediator for the message that would be
> > > needed to restart the original action and would redirect the user back
> > > there.
> > >
> > > Has anyone else tried to solve this problem differently? similarly? more
> > > or less happily?
> > >
> > > thanks,
> > > --Michael

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/about/struts/

Re: Workflow Control and Action Reuse (newbie)

Posted by Ted Husted <hu...@apache.org>.
I would suggest that if you feel a need to reuse an Action, or forward a
request between Actions, then there might be too much business logic
embedded in the Actions.

The best use (I've found) for Actions is as simple adapters between HTTP
and your business logic. The only "work" an Action should do is analyze
the outcome of a business logic operation, and then to relay any errors
and route to the next view. If it is doing more work than that, it is no
longer in the Web tier, and your business logic could not be used in
another environment or easily subjected to Unit Testing.

I would say that the actual work should be taking place in A,B,C,D
objects that can be called by any Action that needs them. Whatever data
they need can be removed from the Servlet contexts, and passed as simple
parameters. The Action just needs to handle any errors or flow control
decisions. If either decision is complex, then that is an indication
that another helper is needed ;-)

Some business logic objects can be instantiated in the Action as part of
the request. Another approach would be to instantiate them once in the
application context, as Struts does with the ActionMappings and message
resources. Any Action could then pass a data bean back and forth as
needed with the client's data.

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/about/struts/


Michael Baldwin wrote:
> 
> I just started looking at struts 4 days ago, so please  bear with me...
> 
> This question really gets into workflow management.
> 
> I have a certain action that would be reused frequently across my site.
> I want to route to that action from many other actions and then return
> both control and information back to the calling action.  i.e., Actions
> A,B,C and D transfer control to Action x.  Once x has done its work, I
> need for x to route control back to A, B, C or D appropriately and to
> provide some "new" request information that was gathered during Action x
> to these Actions A,B,C or D.  The problem is that actions A,B,C and D
> all have different interfaces on their beans so it might be a little
> challenging to have 'x' simply know which methods on bean A,B,C or D to
> use. I think I might want to use a mediator to handle the interactions
> between the beans for A,B,C,D and X
> 
> i.e. action flow control works like this...
> A <--> x
> B <--> x
> C <--> x
> D <--> x
> 
> It seems to me that one way to do this is to:
> 1. Have A, B, C or D place a Mediator-pattern object in the request
> 2. Have A,B,C and D forward (& redirect the request) to action x.
> 3. Action x would look for the Mediator object under a certain key and
> would use the mediator to access information from bean A,B,C or D while
> x does its work.
> 4. When x finishes, it would then update the mediator which would update
> the original Bean used by A,B,C or D
> 5. Finally, x would ask the mediator for the message that would be
> needed to restart the original action and would redirect the user back
> there.
> 
> Has anyone else tried to solve this problem differently? similarly? more
> or less happily?
> 
> thanks,
> --Michael