struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From th...@part.net
Subject Re: setting input for action
Date Tue, 01 Jul 2003 09:08:23 GMT
There are probably > 100 ways to solve this problem but based on the 
information provided this is what I would suggest. 

First of all, it sounds like you need to abstract some of the logic from 
your actions into a business framework of sorts, or you may already have 
something you need to adapt to (???). In either case, you want something 
that is easy to configure, and easy to access from your struts actions. You 
also want an API that is convenient for use within the struts action 
environment. One possibility is illustrated in the following sample code 
which includes a fictitious business api (BizManager), a 
org.apache.struts.action.PlugIn, and a 
org.apache.struts.actions.DispatchAction (NOTE: this is by no means meant to 
be a complete solution, and it assumes you know all about struts 
configuration and everything else I don't attempt to explain ;): 

======================================= begin sample code 

public interface BizManager
{
 void   create(Object valueObject) throws BizException;
 void   update(Object valueObject) throws BizException;
 Object select(Key id) throws BizException;
 Object delete(Key id) throws BizException;
} 


public class BizManagerPlugIn
implemente Plugin, BizManager
{
 public static final String KEY="BizPlugin"; 

 private String thisConfigParam;
 private String thatConfigParam;
 private String theOtherConfigParam; 

 public void setThisConfigParameter(String param){...}
 public void setThatConfigParameter(String param){...}
 public void setTheOtherConfigParameter(String param){...} 

 public void init(ActionServlet servlet, ModuleConfig config)
 {
   ...
   servlet.getServletContext().setAttribute(KEY, this);
 }

 public static final BizManager getBizManager(ActionServlet servlet)
 {
   return (BizManager) servlet.getServletContext().getAttribute(KEY);
 } 

 //~ ---------------------------------------- BizManager API
 // NOTE: you should strive for a very lightweight
 // implementation here. If your business requirements
 // are very simple then you can implement your entire
 // business framework here and it should be managable
 // (i.e. maintainable). However, for more complex requirements
 // you will want to invest some effort into creating a
 // reusable business system. Then you will simply need to
 // adapt (or pass-through) to it here.... 


 public void create(Object valueObject) throws BizException
 {
   ...
 } 

 public void update(Object valueObject) throws BizException
 {
   ...
 } 

 public Object select(Key id) throws BizException
 {
   ...
 } 

 public Object delete(Key id) throws BizException
 {
   ...
 }
} 

public class BizDispatcher
implements DispatchAction
{ 

 public ActionForward prepareFormForEdit(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
   throws Exception
 {
   DynaActionForm dForm = (DynaActionForm)form; 

   BizManager mgr=BizManagerPlugIn.getBizManager(getServlet()); 

   Key key=new Key((String) dForm.get("key"));
   Object valueObject=mgr.select(key); 

   BeanUtils.copyProperties(dform, valueObject); 

   ... 

   return mapping.findForward("editForm");
 } 


 public ActionForward prepareFormForAdd(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
   throws Exception
 {
   DynaActionForm dForm = (DynaActionForm)form; 

   BizManager mgr=BizManagerPlugIn.getBizManager(getServlet()); 

   ... 

   return mapping.findForward("addForm");
 } 


 public ActionForward processEdit(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
   throws Exception
 {
   DynaActionForm dForm = (DynaActionForm)form; 

   BizManager mgr=BizManagerPlugIn.getBizManager(getServlet());

   ... 

   Object valueObject=new Object();
   BeanUtils.copyProperties(valueObject, dform); 

   ... 

   mgr.update(valueObject); 

   ... 

   return mapping.findForward("editAcknowledgementPage");
 } 


 public ActionForward processAdd(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
   throws Exception
 {
   DynaActionForm dForm = (DynaActionForm)form; 

   BizManager mgr=BizManagerPlugIn.getBizManager(getServlet());

   ... 

   Object valueObject=new Object();
   BeanUtils.copyProperties(valueObject, dform); 

   ... 

   mgr.create(valueObject); 

   ... 

   return mapping.findForward("addAcknowledgementPage");
 }
}
======================================= end sample code 


I hope this helps. Sorry if I have mistaken your problem in any way, but 
from what information you've given I think this should provide some benifit 
(???). Good luck! 

Troy 


Fedor Smirnoff writes: 

> Hi I have a following situation: 
> 
> 2 .jsp views (New and Edit) using 
> 1 actionform (Product1)
> calling 2 separate actions(1 each)
> 1 is editAction product 
> 1 is newAction product
> both actions will call third SaveAction at the end 
> 
> However depending on which Action(new or edit) user
> came from, different functions will be performed by
> SaveAction. Since I cannot define one input in struts
> config for SaveAction, how can I find out which action
> the call came from to SaveAction ? so I know which
> methods to call and which .jsp to return to in case of
> errors?  Is there a way to do it, or I am going
> completely wrong about it and there is a better way?
> Thank you. 
> 
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org 
> 
 

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


Mime
View raw message