Sorry - this is a bit of a long example. It's my
attempt to implement some chaining logic using a
generic chainToPage class.
Seems rather long winded to me - but it sure works &
doesn't do anything sneaky involving struts.

In this example the linkList page forwards to
linkMaint & tells linkMaint to return to linkList
(could be any page).

Features
- Only the standard forwardTo call is used.
- All transfers of control use <forwards name=...
names defined in struts-config.xml.

I use terminology of a boss page transferring to a
slave page & telling it where to go on completion,
there must be better terms I'm sure.
boss page  - linkList
slave page - linkMaint

-----example attached.


 
--- Peter Alfors <[EMAIL PROTECTED]> wrote:
> There are a couple options I can think of:
> 
> 1. use request.getHeader("Referer")
> 2. add a property to your generic class that holds
> the return link.
> Then have the calling class set it.
> 3. you could use javascript to back up, but that is
> ugly...
> 
> HTH,
>     Pete
> 
> >Hi,
> >
> >I have a generic action class that I want to set up
> that when the class
> is
> >successful it send the user back to the page from
> which they came. How
> do I
> >get that information? And, how would I structure my
> class to allow it
> to
> >send the user back?
> >
> >Thanks for any info.
> >
> >Alex
> 
> > --
> To unsubscribe, e-mail:  
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com

======================
Struts config.
linkList can transfer to maint or create page.
In the following code example the linkMaint page returns to the linkList page.
This config would allow it to go on to /pageAfterMaintFinished.do as well.
 

                        <!-- Links -->
                        <!-- LinkList uses ChainToPage as a boss -->
        <action path="/biff1LinkList"
                    type="biff1.Biff1LinkListAction"
                  name="biff1LinkListForm"
            scope="request"
            validate="false"
                input="/Biff1LinkList.jsp">
            <forward name="create" path="/biff1LinkCreate.do"/>
            <forward name="maint"  path="/biff1LinkMaint.do"/>
        </action>

                        <!-- LinkList uses ChainToPage as a slave -->
        <action path="/biff1LinkMaint"
                    type="biff1.Biff1LinkMaintAction"
                  name="biff1LinkMaintForm"
            scope="request"
            validate="false"
                input="/Biff1LinkMaint.jsp">
            <forward name="list" path="/biff1LinkList.do"/>
            <forward name="maintFinished" path="/pageAfterMaintFinished.do"/>
        </action>


======================================================================
In the boss action class (linkList) - transfer to the slave (linkMaint) 
--------------------------
                // 1st parm is the chain ID of the thing I want to go to
                //     maybe overkill - it just a enables a check that boss & slave 
match each other.
                // 2nd parm is the local forward name (defined in struts-config for 
the slave) which the
                //     slave is to forward to.
        ChainToPage chainToPage = new ChainToPage(Biff1LinkMaintAction.CHAIN_ID, 
"list");
                //
                // Boss attributes - things that the slave should not look at.
                // Things the boss class will use once the slave has returned.
                // Here it's selected options that must be re-established when the list
                // is redisplayed.
        chainToPage.setBossAttribute("selectedLinkSelectionOption", 
selectedLinkSelectionOption);
        chainToPage.setBossAttribute("selectedReviewDisplayTypeOption", 
selectedReviewDisplayTypeOption);
                //
                // Slave attributes - data being passed to the slave. Key values.
                // Since we have to have some stuff in the session we might
                // as well use it rather than the request. 
                // 
        chainToPage.setSlaveAttribute("selectedLinkID", selectedLinkID);
                // Clear out old stuff, add new one.
        request.getSession().removeAttribute(ChainToPage.SESSION_KEY);
        request.getSession().setAttribute(ChainToPage.SESSION_KEY, chainToPage);
                // forward to the slave.
        return forwardTo(mapping, "maint");
----------------------
In the boss action class (linkList) - when started from another page 
See if transfer is from the the slave (linkMaint) (ie. if it's the
ChainId we recognise. We re-set items on the form to the saved values.
----------------------
        ChainToPage chainToPage = (ChainToPage)
                                                
request.getSession().getAttribute(ChainToPage.SESSION_KEY);
        if (chainToPage != null && 
chainToPage.getChainId().equals(Biff1LinkMaintAction.CHAIN_ID)) {
                String p1 = (String) 
chainToPage.getBossAttribute("selectedLinkSelectionOption");
                if (p1 != null) {
                        thisForm.setSelectedLinkSelectionOption(p1);
                }
                String p2 = (String) 
chainToPage.getBossAttribute("selectedReviewDisplayTypeOption");
                if (p2 != null) {
                        thisForm.setSelectedReviewDisplayTypeOption(p2);
                }

                        // get rid of the thing as soon as possible!
                request.getSession().removeAttribute(ChainToPage.SESSION_KEY);

        }
===============================
In the slave class (linkMaint)
---------------
                // ChainId so boss & slave can check the object is the expected one.

        public  final static String CHAIN_ID  = "Biff1LinkMaint";

-----------------
When starting up
----------------
                //
                // get our object from the session.If it has our ChainId we can 
continue.
                //
        ChainToPage chainToPage = 
(ChainToPage)request.getSession().getAttribute(ChainToPage.SESSION_KEY);
        if (chainToPage == null || !chainToPage.getChainID().equals(CHAIN_ID)) {
                //--error
        }

                //--get page to forward to when completed.
        String chainTo = chainToPage.getSlaveForwardTo();

                        //Get data passed from the boss class.
        String selectedLinkID = 
(String)chainToPage.getSlaveAttribute("selectedLinkID");
        if (selectedLinkID == null || selectedLinkID.equals("")) {
                //---error
        }
-----------------
On completion 
------------------
        return forwardTo(mapping, chainTo);


===============================
ChainToPage class that gets added to the session
---------------------------
package biff1;
        import java.util.HashMap;
        import java.util.Iterator;

public final class ChainToPage  extends Object {
        public  final static String SESSION_KEY = "chainToPage";
        private final static String THIS_NAME = "ChainToPage";

        private String chainID  = null;
        private String slaveForwardTo = null;
                //
                // Data that the boss class wants saved in the session.
                // slave should not touch this stuff.
                // 
        private HashMap bossAttributes  = new HashMap(20);
                //
                // Data that the boss class puts in the session for the
                // slave to use. (Slave could return info here too).
                // 
        private HashMap slaveAttributes = new HashMap(20);
                //
                // Chain ID & page the slave must forward to can't be changed.
                //
        ChainToPage(String chainID, String  slaveForwardTo) {
                this.chainID  = chainID;
                this.slaveForwardTo = slaveForwardTo;
        }

                // no setter - on constructor
        public String getChainID() {
                return chainID;
        }
                // no setter - on constructor
        public String getSlaveForwardTo() {
                return slaveForwardTo;
        }
        /**
        * boss Attributes
        */
        public void setBossAttribute(String key, Object value) {
                bossAttributes.put(key, value);
        }
        public Object getBossAttribute(String key) {
                Object result = bossAttributes.get(key);
                if (result == null) //warning
                return result;
        }
        /**
        * slave Attributes
        */
        public void setSlaveAttribute(String key, Object value) {
                slaveAttributes.put(key, value);
        }
        public Object getSlaveAttribute(String key) {
                Object result = slaveAttributes.get(key);
                if (result == null) // warning  - getSlaveAttribute: not found -r 
key="+ key
                return result;
        }

                // use for debugging only
        public void printAttributeNames(String caller) {
                dbmd("printAttributeNames: for caller="+ caller +"-----------");
                dbmd("       slaveForwardTo="+ slaveForwardTo +" chainID="+ chainID);
                Iterator iii = bossAttributes.keySet().iterator();
                while (iii.hasNext()) {
                        String key = (String) iii.next();
                        dbmd("       boss key=" + key);
                }
                Iterator jjj = slaveAttributes.keySet().iterator();
                while (jjj.hasNext()) {
                        String key = (String) jjj.next();
                        dbmd("       slave key=" + key);
                }
                dbmd("printAttributeNames: end ------------------------------");
        }

} // end Class ChainToPage

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to