Using WebObjects DirectActions -- pt. 3
Calling a DirectAction with values from a form
This is the 3rd in a series, part 1 is here and part 2 is here. We’ve looked at the composition of a DirectAction URL, and how to retrive passed values. This time we’re going to look at how to call a DA with values from a form.
We are going to extend the examples we used last time so fire up your IDE (Eclipse I hope) and lets get started.
First we need to modify our Main component, edit the Main.html so it looks like this:
<html>
<head>
<meta name="generator" content="WOLips Core">
<title>New</title>
</head>
<body bgcolor=#FFFFFF>
<webobject name=Form1>
Usrename: <input type=text name=username>
<br />
Password: <input type=password name=password>
<br />
<input type=submit>
</webobject>
</body>
</html>
Change the Main.wod so it looks like this:
Form1: WOForm {
directActionName = "helloWorld";
actionClass = "DirectAction";
}
Save and run the application. After the Main page loads, enter values in the two form fields and hit the submit button. You should see something like this in the console:
Form Values: {password = ("pass"); username = ("dave"); }
A couple of things to note: The only WO component on this page is the WOForm, it is bound to the helloWorld directActionName, and the DirectAction actionClass. All of the other form elements are just plain HTML.
You’ll also notice that the text field values are not retained -- after you submit the form the text fields are blank. Lets look at how we might fix that:
- In WebObjectsBuilder convert the text fields to WOTextFields by clicking on the Make Dynamic button in their Inspector windows.
- Add a
usernameandpasswordvariable of type String to your Main.java - Bind the
usernamevariable to the username WOTextField, and thepasswordvariable to the password WOTextField - Modify the
helloWorldActionin theDirectActionclass so it looks like this:
public WOActionResults helloWorldAction() {
NSDictionary dict = this.request().formValues();
System.out.println("DA values: " + dict);
WOComponent page = pageWithName("Main");
String username =
(String)this.request().formValueForKey("username");
String password =
(String)this.request().formValueForKey("password");
page.takeValueForKey(username, "username");
page.takeValueForKey(password , "password");
return page;
}
Now when we build, run, and submit the form, the form values should be retained. This little example shows how to grab the values from a form, and pass them to a page.
Before we go, lets try a little experiment, add a WOString to your Main component and bind it to session.sessionID (WOBuilder may complain that the This binding value is not defined -- just click the Don’t Add button and move on).
When you are done, reload the Main page and submit the form a couple of times. What happens? If everything is working you should see something like the following appended to the URL:
wosid=e3ef6ofP1badQRlQtJdIew
Note: This will only work if you haven’t set setStoresIDsInCookies(true); and setStoresIDsInURLs(false); in your Session constructor.
wosid stands for WO Session ID and this demonstrates the mechanism that DirectActions use to track a Session. So, although you don’t need to trigger a Session from your DirectActions (you are free to manage state yourself) you can get one just by asking for it. This can be very helpful as we will see later.
- ← Previous
Happy, happy... - Next →
Giant inflatable parade animals made from corn husks?