Calling a DirectAction with values from a WOHyperlink
In the last segment we looked at using a form to submit values into a DirectAction, this time we’ll look at binding values to a WOHyperlink.
According to Apple’s Dynamic Elements Reference, the bindings for a WOHyperlink are:
action, href, pageName, directActionName, actionClass, fragmentIdentifier, string, target, disabled, secure, queryDictionary
The bindings in bold are the ones we are interested in:
- directActionName – The name of the action minus "Action" (i.e. "helloWorld").
- actionClass – The name of the class containing the action (in our examples we’ve been using the default: "DirectAction").
- queryDictionary – A dictionary of key/value pairs to append to the URL.
Clear? OK, it’s time to get started. As always, we are going to extend the previous examples, so fire up your IDE and we’ll begin.
First, add a new WOHyperlink to the Main component (somewhere outside the form). Bind its directActionName
to "hyper"
and its actionClass
to "DirectAction"
(don’t forget the " "s).
Next, create the hyperAction
that’s bound to our WOHyperlink. Add the following to the DirectAction
class:
public WOActionResults hyperAction() { WOComponent page = pageWithName("Main"); System.out.println("Hyperlink values: " + this.request().formValues()); return page; }
This is not complex code it just exposes what is happening when this action is called.
Build and run the application. When the Main page loads, click on the new hyperlink. The page should reload and you should see this in the console:
Hyperlink values: {}
Now, lets attach a value to the WOHyperlink. In WebObjects Builder select the WOHyperlink and invoke the inspector (cmd+1). Add a new binding (use the small [+] button in the upper right corner of the Inspector window, and select Add binding). Give it a binding of ?name
and a value of "david"
. The Main.wod snippet for the WOHyperlink should look something like this:
Hyperlink1: WOHyperlink { ?name = "david"; actionClass = "DirectAction"; directActionName = "hyper"; }
Save the component and reload the page, click on the link again and you should see this in the console:
Hyperlink values: {name = ("david"); }
You can add additional key-value pairs to the WOHyperlink by repeating the Add binding step above or by directly editing the Main.wod. For instance, the following WOHyperlink .wod snippet:
Hyperlink1: WOHyperlink { ?age = "10"; ?name = "david"; actionClass = "DirectAction"; directActionName = "hyper"; }
Results in this output:
Hyperlink values: {name = ("david"); age = ("10"); }
Alternately, we can add the values to the WOHyperlink all at once in by using the queryDictionary
. Open the Main.java file and add the following method:
public NSDictionary hyperDictionary() { NSMutableDictionary dict = new NSMutableDictionary(); dict.takeValueForKey("david", "name"); dict.takeValueForKey("10", "age"); return dict; }
Delete the two bindings you added to the WOHyperlink in the last step, and bind the hyperDictionary
method to the queryDictionary
binding. The section of Main.wod for the WOHyperlink should look like this:
Hyperlink1: WOHyperlink { actionClass = "DirectAction"; directActionName = "hyper"; queryDictionary = hyperDictionary; }
OK, were almost done, build and run the application once again. Click on the link and you should see the same entry in the console as above:
Hyperlink values: {name = ("david"); age = ("10"); }
At this point you should have pretty much everything you need to play with DirectActions. We’ve looked at how to configure your application, trigger an action, decipher a DirectAction URL, grab values from a form, pass values with your hyperlinks, and get the use of a session if you need one. Next time we’ll look at combining Component actions and DirectActions using a WORedirect.