Who Says EJB 2.1 is Too Hard?, Page 3
Create a Demo Client in Java Studio Creator
Almost there now. Throw a quick project together and a couple of pages to demonstrate the new easy binding features for EJBs in Creator. You will demonstrate it using Data Tables, but the same EJB methods can be bound to other controls using the same tricks. For more information on general binding in Java Studio Creator, see the tutorials at http://developers.sun.com/prodtech/javatools/jscreator/ea/jsc2/learning/tutorials/index.html.
First, get a simple project and page to list all the people:
- At the Creator welcome page, click Create New Project.
- Select Web and JSF Web Application from the Choose Project dialog. Click Next.
- Choose a name (like PeopleDemo) and a location for the project. Click Finish.
- In the resulting Page1 file that comes up, drag a Table control from the Palette on the left onto the page.
- Rename the "Table" label to read People.
- Now, for the cool part. Go to the Servers tab in the top left pane and find the listAll method from the CreatorDemoEJB (if you have not expanded it already, expand the CreatorDemoEJB and the ejb/PeopleEJBBean under that until you see the methods).
- Drag the listAll method onto the data table you just created. This binds the results of that method call to the table; you will see the fields change in response to the PersonDTO class fields.
- Pretty impressive, but the columns are probably in the wrong order, plus the peopleList static property shows up (which you don't want), and you also can give it some clues about the representation of the data you want:
- Right-click on the table and select Table Layout.
- Select peopleList and click the < button to remove it.
- Select firstName and click the Up button until it is at the top.
- Do the same for lastName.
- You might want to arrange the homeTel and workTel numbers together as well.
- Feel free to select each column definition in turn and give it a better Header Text entry (which will be displayed in the column header).
- Also, change the onVacation field to be represented as a checkbox: select onVacation in the columns and then change the Component Type to Checkbox.
- Click OK. You should see the changes reflected in the data table.
Note: If you get a dialog asking you to select a target, make sure you select the whole table, probably called table 1).
Now, run the project (Run->Run Main Project from the menu). You should see a page like this:
If you have not yet played with the new Java Studio Creator 2 controls, go ahead and click on the column headings; this allows you to sort the data displayed in the table by the different columns (and, by using the + symbols that appear after you first select a sort order, you can create a sort hierarchy as well).
A More Involved Example
So, you just bound the results from a simple EJB list method directly into a data table. Very neat, but what if you want to be a little more selective about what you show?
If you remember, you created a method called listIncomeAbove that took a value to filter the details returned by. Create a page that uses that.
- In the Projects tab in the bottom right pane, right-click on the Web Pages node and select New->Page....
- For the File Name, type ListIncomeAbove. Click Finish.
- In the Projects tab again, find the new page, right-click on it and select Set As Start Page (this will make testing it easier).
- This time, drag a Static Text control, Text Field control, and Data Table onto the page so it looks like this:
- You should rename the ID of at least the text field to be something like amountInput. Renaming your controls to a meaningful name at least for all of the input ones is a good idea because it makes your page code easier to read. Select the control, click properties, and change the top field in the properties pane called ID to read amountInput.
- Find the listIncomeAbove EJB method and drag it over the data table.
- Follow Step 8 from the List All example above to change the layout of the table and make it look like the List All page.
- Now, right-click on the amountInput text field and select Auto Submit on Change, so that when you enter a value here, it will fire a change event in the page bean.
- Double-click on the inputAmount text field and Creator will drop you into editing the event for that text field.
- Where it says //TODO: Replace with your code, replace the line with the following:
- When you dragged the listIncomeAbove method onto the data table, Java Studio Creator made this peopleEJBRemoteListIncomeAbove1 reference for you, which you can now use to tell it the amount you want to filter by, and then refresh the object (that will update the data in the table). It's that easy.
- Run it to see the results. At first, you will see a list of all of the people in the system (the income amount filter is 0 by default), but if you type in 100000 to the income above field and click return, it will filter down to two names with income above 100000.
double amount = Double.parseDouble((String)this.amountInput.getValue()); this.peopleEJBRemoteListIncomeAbove1.setAmount(amount); this.peopleEJBRemoteListIncomeAbove1.refresh();
Well, once again you barely scratched the surface of what is possible, but if you think about the ground covered in this article, it is quite impressive. You have put together an EJB API, deployed it to the App Server Instance in Java Studio Creator, consumed it in Creator, and then bound it to a control in a JSF page.
More to the point, you have done this without seeing a deployment descriptor, Home and Remote interface classes, JNDI lookup code, or anything else that might be considered tricky or confusing. The new EJB features in Java Studio Creator 2 truly do extend the component model and ease of use data bindings into the world of full EJB 2.1 architecture.
Many people are eagerly awaiting the release of EJB 3.0 to help simplify EJB development, but with suitable use of the right IDE tools, simple development using EJBs is available right now, I also look forward to EJB 3.0, but it's hard to imagine how much easier it will make things for the demo we have seen.
About the Author
Dick Wall is a Lead Systems Engineer for NewEnergy Associates, A Siemens Company based in Atlanta, GA that provides energy IT and consulting solutions for decision support and energy operations. He can be reached for comment on this and other matters at firstname.lastname@example.org. He also co-hosts the Java Posse with Tor Norbye and Carl Quinn, a podcast devoted to Java News and Interviews, which can be found at http://javaposse.com.