macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

An Introduction to WebObjects
Pages: 1, 2, 3

Now that there are a couple of variables, you can bind them to your view by simply click-dragging from the variable name to the component you wish to bind. You will want to bind the value attribute of the WOTextField to your variable. Do this for both the username and password.

You'll also want to change the WOTextField for the password to a Password Field. You can accomplish this by selecting the WOTextField and changing the Inspector Panel to show the Static Inspector.

You can then select to make the WOTextField a Password Field. This will hide the user's input behind bullets while typing.

NOTE: By changing to a Password Field, you are not encrypting the information. You are simply changing from an HTML Text Field to an HTML Password Field. As always, unless the connection between the client and the server is encrypted, the text in this field will be sent across the network in clear text.

The final three steps in the page layout are to add a Submit button, add a Submit action, and to place the panel inside of a WOForm.

  • To add the Submit button, click on the WOSubmit button in the toolbar.
  • To add the Submit action, control-click (or right-click) in the Object Browser and select the "Add Action to Main" option. Name it submit and return null for the Page.

    You should then bind submit to the Submit button. You can accomplish this as before, by simply click-dragging from submit to the Submit button. You will want to bind the action attribute.

  • Finally, you want to "wrap" the login panel and Submit button in a WOForm. The WOForm button can be found directly to the left of the WOTextField button. By selecting the section of the page you wish to "wrap" (the table and Submit button) and clicking on the WOForm button, you will enclose the login panel inside of the WOForm. If you do not use a WOForm, the Submit button will not work correctly.

I'm sure some of you are asking, "Where's the EOF stuff?" Well, it's coming, but you need to crawl before you can walk. For now, you need to go back to Project Builder and enter some code.

Using Project Builder

In your Main.java file, you'll want to add some code. First, you'll need somewhere to store the username and password for your users. For this example, you'll use an NSMutableDictionary.


    protected NSMutableDictionary userDictionary;
You'll also want to initialize that dictionary at some point. Here it's being initialized with a couple of values, in the constructor.

    public Main(WOContext context) {
        super(context);
	
        String users[] = new String[] {"Andy","Beth"};
        String passwords[] = new String[] {"alpha", "beta"};
        userDictionary = new NSMutableDictionary(passwords, users);
    }

NSMutableDictionary is a simple object that allows you to store and retrieve "values" using "keys", following the Key-Value Coding Pattern.

You're almost there, but you still need to give the Submit button (which is bound to our submit method) a little more oomph. In your Main.java file, locate the submit method that WOB created for you. When the user clicks the Submit button, you'll do a quick lookup in the userDictionary to see if the information is correct.


    public WOComponent submit()
    {
	String s;
	
	s = (String) userDictionary.valueForKey(username);
	if (s != null && s.equals(password)) {
	    System.out.println("correct");
	}
	else {
	    System.out.println("incorrect");
	}
	return null;
    }
    

When the user clicks on the Submit button, WebObjects is going to do its magic and call the submit method. It is also going to "push" the values the user inputs into your username and password variables. Therefore, you'll be able to compare the input values to the values in your userDictionary.

Challenge: add a feedback variable to your Main page, bind it to a WOString , and provide some feedback to the user instead of printing out to the console.

As you'd quickly discover, keeping the user's information in a hard-coded NSMutableDictionary is not very useful (even though it is mutable). Considering that the userDictionary object is only valid while the program is running, you can't even rely on changing a user's password. The second you shut down the application, the changes would be lost. You could move the code around the project, creating special storage components, moving it into the Session object or even the Application object, but it's kind of like putting earrings on a pig.

Simple "Login Window" Example Using WO/EOF

Create an EOModel for a User

Open EOModeler and create a new Model. You will be asked if you'd like to create a connection using JDBC, JNDI, or None. Select the JDBC option and enter the information for your database. I've chosen to use OpenBase, but you're free to use any database that has a JDBC driver available.

One of the beautiful features of EOModeler is that it is database-independent. EOF will handle generate SQL statements for you, which will allow you to move from one database vendor to another with little effort. This feature will also allow you to concentrate on your project and not the underlying database calls.

I recently had a personal experience with this feature moving a site running on MySQL to OpenBase. I was able to accomplish the "port" in about 15 minutes. It took a little more time than I had planned because I had two raw SQL calls in my project to use MySQL's IN BOOLEAN MODE search feature.

Okay, enough stories, back to work. You should create a new Entity and name it User. An Entity is similar to a Table in the database, except for the fact that an Entity will become an Object; an EOEnterpriseObject, to be specific.

Once you have created your Entity, you'll want to provide information to EOModeler to create the Object-Relational Mapping. Make sure the Inspector Panel is open. You'll need to provide the Table name that you wish to relate to this Entity. Don't worry about the Class Name, EOGenericRecord is fine for now. Database purists can feel free to name their Tables in UPPERCASE.

You should now add a few Attributes to your Entity. When adding Attributes, you should relate them to a Column in your database. You will want to add (the External Types will depend on your database vendor):

  • username: Value Class - String / External Type - varchar
  • password: Value Class - String / External Type - varchar
  • pk: Value Class - Number / External Type - integer

Pages: 1, 2, 3

Next Pagearrow