Using WebObjects DirectActions – pt. 5

Calling a DirectAction with values from a WORedirect

WORedirect is useful when you need a regular Component action (i.e. non DA) to lead somewhere other than another WOComponent (i.e. If you need to perform some cleanup and return a company homepage when a logout link is clicked).

Luckily for us, a WORedirect can also easily allows us to redirect to a DirectAction, as the following code demonstrates:

String searchString; 		//assume exists
String category;		//assume exists
String pageOfResults = "0"
 
public WORedirect performSearch() {
    NSMutableDictionary dict = new NSMutableDictionary();
    dict.takeValueForKey(searchString, "searchString");
    dict.takeValueForKey(category, "category");
    dict.takeValueForKey(pageOfResults, "page");
    String url = context().directActionURLForActionNamed(
                 "search", dict ); 
    WORedirect redirect = new WORedirect(context());
    redirect.setUrl(url);
    return redirect;
}

This hypothetical action is bound to a WOSubmitButton in a WOForm on a WOComponant. It is important to note that its return type is WORedirect instead of the usual WOComponent.

It function is pretty clear, it creates a new NSMutableDictionary and populates it with the values and keys that we need to append to our DirectAction URL. The single line of code does most of the heavy lifting:

String url = context().directActionURLForActionNamed( 
                  "search", dict );

It creates the DirectAction url (in this case for the searchAction) for us and append the values from our NSMutableDictionary. Finally we set the url for the WORedirect and return it.

The directActionURLForActionNamed comes from WOContext and it’s API can be found here.

Well, I think this will be the last DirecAction specific post. I’ve covered pretty much everything I set out to, so I think I’ll take a break an look for a different topic to examine. If anyone has any suggestions, let me know.

9 thoughts on “Using WebObjects DirectActions – pt. 5

  1. Thanks for the clear examples. Very helpful.

    For completion it would be good to see pt 6: Using WebObjects Direct Actions (moving between DirectAction and WOSession/Action based Components).

    I also wouldn’t mind a topic on setting up a secure WebServices application (dev . to deploy) and utilising its services securely from a standard site (both DirectAction and normal session based).

    Thanks again.

  2. The directActionforURLNamed() function returns parameters with & separating the dictionary values. Passing this directly to WORedirect does not work. Am I doing something wrong, or is there something missing above?

  3. Hello David LeBer, I watched your WO tutorials about login and done the way you told. However I have an issue with logout. I am not sure how to do it. Is it possible for you to make small tutorial as continuation to the previous one, which demonstrates the logout process.

    Thank you.
    P.S: Your tutorial was a life saver.

  4. For logout, you can terminate the session, and than redirect to a Direct Action logout page where there you may tell users like “you log out successfully blah blah blah and perhaps provide a link to the login page.


    session.terminate();
    WORedirect page = new WORedirect( this.context() );
    page.setUrl(
    context().directActionURLForActionNamed(
    “logout”, new NSDictionary(Boolean.FALSE, “wosid”)));
    return page;

Comments are closed.